2
創建了我自己的字符串類(即明顯用於作業),並且在我的兩個操作符上出現了奇怪的語法錯誤。我的平等和添加運算符聲明我有太多的參數(即在我的.h文件中),但是然後聲稱該方法甚至不屬於我的.cpp文件中的類!操作員對此功能的參數太多?
我甚至將平等運算符作爲朋友,但intellisense仍給我相同的錯誤消息。
有誰知道我在做什麼錯?
friend bool operator==(String const & left, String const & right);
string.h中
bool operator==(String const & left, String const & right);
String const operator+(String const & lhs, String const & rhs);
string.cpp
bool String::operator==(String const & left, String const &right)
{
return !strcmp(left.mStr, right.mStr);
}
String const String::operator+(String const & lhs, String const & rhs)
{
//Find the length of the left and right hand sides of the add operator
int lengthLhs = strlen(lhs.mStr);
int lengthRhs = strlen(rhs.mStr);
//Allocate space for the left and right hand sides (i.e. plus the null)
char * buffer = new char[lhs.mStr + rhs.mStr + 1];
//Copy left hand side into buffer
strcpy(buffer, lhs.mStr);
//Concatenate right hand side into buffer
strcat(buffer, rhs.mStr);
//Create new string
String newString(buffer);
//Delete buffer
delete [] buffer;
return newString;
}
同樣適用於'operator +',無論它是'朋友'還是敵人。 – juanchopanza 2013-04-18 05:48:46
@NPE - 哇。非常感謝!! – MrPickle5 2013-04-18 05:51:14
編輯:忘記刪除我的班級內的方法聲明。一切都很好。再次感謝你!! – MrPickle5 2013-04-18 05:53:46