【发布时间】:2012-03-05 03:13:23
【问题描述】:
我有一个带有重载运算符的类:
IPAddress& IPAddress::operator=(IPAddress &other) {
if (this != &other) {
delete data;
this->init(other.getVersion());
other.toArray(this->data);
}
return *this;
}
当我尝试编译时:
IPAddress x;
x = IPAddress(IPV4, "192.168.2.10");
我收到以下错误:
main.cc: In function ‘int main()’:
main.cc:43:39: error: no match for ‘operator=’ in ‘x = IPAddress(4, ((const std::string&)(& std::basic_string<char>(((const char*)"192.168.2.10"), ((const std::allocator<char>&)((const std::allocator<char>*)(& std::allocator<char>())))))))’
IPAddress.h:28:20: note: candidate is: IPAddress& IPAddress::operator=(IPAddress&)
但是,这两个工作正常(尽管它们对我没有任何用途):
IPAddress x;
IPAddress(IPV4, "192.168.2.10") = x;
-
IPAddress x;
x = *(new IPAddress(IPV4, "192.168.2.10"));
发生了什么事?我是否假设赋值运算符的工作方式不正确?
【问题讨论】:
标签: c++ methods operator-overloading