运算符这个函数的参数太多了? [英] Operator(s) too many parameters for this function?
问题描述
做我自己的字符串类(即显然为家庭作业),并得到奇怪的语法错误我的两个运算符。我的平等和添加运算符声称我有太多的参数(即在我的.h文件中),但是声明该方法甚至不属于我的.cpp文件中的类!
Made my own string class (i.e. for homework obviously) and am getting odd syntax errors on two of my operators. My equality and add operators claim I have too many parameters (i.e. in my .h file), but then claim the method does not even belong to the class in my .cpp file!
我甚至让平等运算符成为朋友,但是intellisense仍然给我相同的错误消息。
I even made the equality operator a friend, but the intellisense still gives me the same error messages.
有人知道我在做什么错误
Does anyone know what I am doing wrong??
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;
}
推荐答案
c $ c> operator == 以外的类:
You need to define operator==
outside the class:
bool String::operator==(String const & left, String const &right)
^^^^^^^^ REMOVE THIS
如果 operator +
也是一个朋友,它也需要定义为一个自由函数(即类外)。
If operator+
is also a friend, it too needs to be defined as a free function (i.e. outside the class).
这篇关于运算符这个函数的参数太多了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!