句子类
class sentence
{
public:
sentence();
~sentence();
void testing(sentence *& s);
sentence operator+=(char * sentence_to_add);
protected:
node * head;
};
实施(不正确,只测试是否有效)
sentence sentence::operator+=(char * sentence_to_add)
{
cout << "testing" << endl;
return *this;
}
测试所述运营商
void sentence::testing(sentence *& s)
{
char * test_string = new char[5000];
cout << "please enter a string: " << endl;
cin.getline(test_string, 5000, '\n');
s += test_string;
}
G ++编译器错误
sentence.cpp: In member function âvoid sentence::testing(sentence*&)â:
sentence.cpp:124:7: error: invalid operands of types âsentence*â and âchar*â to binary âoperator+â
sentence.cpp:124:7: error: in evaluation of âoperator+=(class sentence*, char*)â
我做错了什么?因为从技术上讲这应该有效。由于左值是指向类对象的指针,因此右值是char数组。所以我很确定操作数不是无效的......
编辑/更新: 原型
sentence & operator+=(const char * sentence_to_add);
实施
sentence & sentence::operator+=(const char * sentence_to_add)
{
cout << "testing" << endl;
return *this;
}
答案 0 :(得分:2)
你的函数需要一个指针,而不是一个对象。只需参考(sentence&
。)
另外,您的运营商应该返回参考,而不是副本。