这个函数的运算符参数太多
Operator(s) too many parameters for this function?
制作了我自己的字符串类(即显然是为了作业),并且我在两个操作符上得到了奇怪的语法错误。我的相等和添加操作符声称我有太多的参数(即在我的。h文件中),但随后声称该方法甚至不属于我的。cpp文件中的类!
我甚至把相等运算符当作朋友,但是智能感知仍然给我相同的错误消息。
有人知道我做错了什么吗?
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==
:
bool String::operator==(String const & left, String const &right)
^^^^^^^^ REMOVE THIS
如果operator+
也是友元,它也需要被定义为自由函数(即在类之外)
相关文章:
- 调用宏时参数太多
- C++中的参数太多
- 参数太多,无法在C++中'FILE* popen()'发挥作用
- 野牛:如何修复这个"参数太多而无法运行'int yylex()'错误?
- C++中的模板参数太多
- 重载运算符<<:此运算符函数的参数太多
- 依赖注入/继承设计模式的构造函数参数太多
- 函数调用的参数太多,预期为 0,有 1。 "InfInt.h"字符串函数
- 函数调用运算符重载的参数太多
- C++中的构造函数参数太多
- Mysql.h c++ 参数太多
- 警告:格式 [-Wformat-extra-args] 的参数太多
- 宏'BOOST_FUSION_ADAPT_STRUCT_FILLER_0'的实际参数太多
- 警告:格式 - fprintf 的参数太多
- 重载运算符的参数太多,visual studio c++
- open()的参数太多
- 错误:函数参数太多
- 尝试重载时语法错误 "<<" :参数太多
- 在最深层的继承类中构造函数参数太多
- 这个函数的运算符参数太多