重载==编译错误c++
Overloading == compiling error c++
我制作了一个简单的 struct
命名坐标来保持坐标,我想检查两个坐标是否相等,所以我研究了如何在另一个线程中进行适当的运算符重载并想出了这 :
#include <iostream>
using namespace std;
struct coord{
int x;
int y;
inline bool operator==(const coord& lhs, const coord& rhs){
if(lhs.x == rhs.x && lhs.y == rhs.y){
return true;
}
else{
return false;
}
}
};
int main(){
coord a,b;
a.x=5;
a.y=5;
b.x=a.x;
b.y=a.y;
if(a==b){
cout<<"Working"<<endl;
}
return 0;
}
但是在编译时我得到了一个看起来像这样的巨大错误:
g++ -c -o obj/main.o main.cpp -I../include
main.cpp:8:62: error: ‘bool coord::operator==(const coord&, const coord&)’ must take exactly one argument
inline bool operator==(const coord& lhs, const coord& rhs){
^
main.cpp: In function ‘int main()’:
main.cpp:24:6: error: no match for ‘operator==’ (operand types are ‘coord’ and ‘coord’)
if(a==b){
^
main.cpp:24:6: note: candidates are:
In file included from /usr/include/c++/4.9.2/iosfwd:40:0,
from /usr/include/c++/4.9.2/ios:38,
from /usr/include/c++/4.9.2/ostream:38,
from /usr/include/c++/4.9.2/iostream:39,
from main.cpp:1:
/usr/include/c++/4.9.2/bits/postypes.h:216:5: note: template<class _StateT> bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
operator==(const fpos<_SenttateT>& __lhs, const fpos<_StateT>& __rhs)
^
这种情况适用于 ‘coord’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’
或 ‘const std::allocator<_CharT>’
等各种其他标准事物,整个错误是 here it is on pastebin.
我希望有人明白这里出了什么问题并能向我解释一下
有两种选择可以解决这个问题:
成员函数
inline bool operator==(const coord& rhs) const {
return (this->x == rhs.x && this->y == rhs.y);
}
当您使用
if(a==b){
在对象 a
上调用函数,函数的参数是 b
。
非成员函数
这可以在struct coord
的定义之外定义。
struct coord{
int x;
int y;
};
bool operator==(const coord& lhs, const coord& rhs){
return (lhs.x == rhs.x && lhs.y == rhs.y);
}
当您使用
if(a==b){
lhs
使用 a
调用函数,rhs
使用 b
。
我制作了一个简单的 struct
命名坐标来保持坐标,我想检查两个坐标是否相等,所以我研究了如何在另一个线程中进行适当的运算符重载并想出了这 :
#include <iostream>
using namespace std;
struct coord{
int x;
int y;
inline bool operator==(const coord& lhs, const coord& rhs){
if(lhs.x == rhs.x && lhs.y == rhs.y){
return true;
}
else{
return false;
}
}
};
int main(){
coord a,b;
a.x=5;
a.y=5;
b.x=a.x;
b.y=a.y;
if(a==b){
cout<<"Working"<<endl;
}
return 0;
}
但是在编译时我得到了一个看起来像这样的巨大错误:
g++ -c -o obj/main.o main.cpp -I../include
main.cpp:8:62: error: ‘bool coord::operator==(const coord&, const coord&)’ must take exactly one argument
inline bool operator==(const coord& lhs, const coord& rhs){
^
main.cpp: In function ‘int main()’:
main.cpp:24:6: error: no match for ‘operator==’ (operand types are ‘coord’ and ‘coord’)
if(a==b){
^
main.cpp:24:6: note: candidates are:
In file included from /usr/include/c++/4.9.2/iosfwd:40:0,
from /usr/include/c++/4.9.2/ios:38,
from /usr/include/c++/4.9.2/ostream:38,
from /usr/include/c++/4.9.2/iostream:39,
from main.cpp:1:
/usr/include/c++/4.9.2/bits/postypes.h:216:5: note: template<class _StateT> bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
operator==(const fpos<_SenttateT>& __lhs, const fpos<_StateT>& __rhs)
^
这种情况适用于 ‘coord’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’
或 ‘const std::allocator<_CharT>’
等各种其他标准事物,整个错误是 here it is on pastebin.
我希望有人明白这里出了什么问题并能向我解释一下
有两种选择可以解决这个问题:
成员函数
inline bool operator==(const coord& rhs) const {
return (this->x == rhs.x && this->y == rhs.y);
}
当您使用
if(a==b){
在对象 a
上调用函数,函数的参数是 b
。
非成员函数
这可以在struct coord
的定义之外定义。
struct coord{
int x;
int y;
};
bool operator==(const coord& lhs, const coord& rhs){
return (lhs.x == rhs.x && lhs.y == rhs.y);
}
当您使用
if(a==b){
lhs
使用 a
调用函数,rhs
使用 b
。