这应该是一个包含一堆运算符和函数的字符串类,包括两个友元函数.这两个对我来说有些麻烦,因为编译器说他们无法访问私有成员.这是我的string.h:
#include <iostream>
#ifndef STR_H
#define STR_H
namespace MyStr
{
class Str
{
private:
unsigned int length;
char *data;
public:
Str();
Str(const Str&);
Str(const char*);
Str(char c, unsigned int db);
~Str();
char* cStr() const;
unsigned int getLength() const;
Run Code Online (Sandbox Code Playgroud)
这里有许多无关紧要的功能......
friend int operator/ (const Str&, char);
friend std::ostream& operator<< (std::ostream&, const Str&);
};
}
#endif /* STR_H */
Run Code Online (Sandbox Code Playgroud)
这是main.cpp:
#include <iostream>
#include "Str.h"
using namespace std;
using namespace MyStr;
ostream& operator<< (ostream& out,const Str& str)
{
for (int i=0; i<str.length; i++) …Run Code Online (Sandbox Code Playgroud) 我正在与C2751编译器错误搏斗,并不太明白究竟是什么导致它.以下小代码产生错误:
#include <iostream>
class A {
public:
A () { std::cout << "A constructed" << std::endl; };
static A giveA () { return A (); }
};
class B {
public:
B (const A& a) { std::cout << "B constructed" << std::endl; }
};
int main () {
B b1 = B (A::giveA ()); // works
B b2 (B (A::giveA ())); // C2751
B b3 (A::giveA ()); // works
}
Run Code Online (Sandbox Code Playgroud)
编译器输出:
consoleapplication1.cpp(21): error C2751: 'A::giveA': the name of …