我创建了一个 myString 类,我正在尝试运行以下代码:
class myString{
char* str;
int len;
public:
myString(char* str1 = " "){
len = strlen(str1);
str = new char[len+1];
strcpy(str, str1);
};
int getLen() const {
return len;
};
char* getString() const {
return str;
};
~myString(){
delete[] str;
};
myString& operator=(myString& orig){
cout << "hello";
if (str == NULL){
delete[] str;
};
str = new char[orig.getLen()];
strcpy(str, orig.getString());
cout << this << endl;
return *this;
};
...
};
int main(){
myString s("bla");
myString k("dingo");
myString g = s;
// s=k; //When this line is commented I get a linking error
...
};
我的问题:
- 为什么“你好”没有被打印出来?
- 为什么该行
s=k
会导致链接器错误?
这是错误:
LINK : c:\users\perry\documents\visual studio 2010\Projects\inheritance\Debug\inheritance.exe 未找到或未由最后一个增量链接构建;执行完整链接 1>main.obj : 错误 LNK2019: 无法解析的外部符号 "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream
&,class myString *)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@PAVmyString@@@Z) 在函数 "public: class myString & __thiscall myString ::operator=(class myString &)" (??4myString@@QAEAAV0@AAV0@@Z) 1>c:\users\perry\documents\visual studio 2010\Projects\inheritance\Debug\inheritance.exe:致命错误LNK1120:1 个未解决的外部因素
谢谢,李