1
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;
}
'只需要一個引用(句子&。)' - 它應該是一個'const語句&'(對於其他超載而言是'const char *')。 –
我仍然得到無效的操作數,但是當我做'*(s)+ = test_string;'它編譯和工作得很好。你能解釋一下嗎? – ZZPLKF
@TonyD:該函數修改它的參數,所以不,它不應該接受一個const引用。儘管你對運營商的投入是正確的。 –