在 C++ 中不匹配 'operator*'
no match for 'operator*' in C++
我正在重载运算符 * 和 + 以从文件中读取一些 class' 变量。我写了下面的代码:
class X {
public:
double getLength(void) {
return length;
}
void setLength( double len ) {
length = len;
}
// Overload + operator to add two Box objects.
X operator*(X temp){
X temp1;
temp1.length = this->length * temp1.length;
return temp1;
}
X operator*(int num){
X temp1;
temp1.length = this->length * num;
return temp1;
}
X operator+(X temp){
X temp1;
temp1.length = this->length + temp1.length;
return temp1;
}
private:
double length; // Length of a box
};
// Main function for the program
int main() {
X ob1; // Declare Box1 of type Box
X ob2; // Declare Box2 of type Box
X ob3; // Declare Box3 of type Box
double result = 0.0; // Store the volume of a box here
ob2.setLength(6.0);
ob3.setLength(12.0);
ob1 = ob2 + 2*ob3;
ob1 = ob2*2 + ob3;
ob1 = (ob2 + 2) *ob3;
cout << "length of Box : " << ob1.getLength() <<endl;
return 0;
}
但是当我尝试编译上面的代码时,出现以下错误:
main.cpp: In function 'int main()':
main.cpp:48:17: error: no match for 'operator*' (operand types are 'int' and 'X')
ob1 = ob2 + 2*ob3;
~^~~~
main.cpp:50:15: error: no match for 'operator+' (operand types are 'X' and 'int')
ob1 = (ob2 + 2) *ob3;
~~~~^~~
main.cpp:27:8: note: candidate: 'X X::operator+(X)'
X operator+(X temp){
我无法理解我的代码中的错误。请帮我解决错误。
当您将运算符作为成员函数时,表达式的左侧是函数调用的对象,右侧是成为参数的对象。
所以根据您的定义:
ob3 * 2:; // Fine: Calls ob3.operator*(2)
2 * ob3; // Not fine, there's no operator* function which takes an int on the left-hand side
你可以使用非成员函数来解决这个问题:
X operator*(int a, X b)
{
return b * a; // Calls b.operator*(a)
}
现在您可以在左侧有一个 int
:
2 * ob3; // Fine: Calls operator+(2, ob3)
我也推荐this operator overloading canonical implementaiton reference。它建议(除其他事项外)您使用 operator*=
,然后根据 *=
实施 operator*
。对于所有二元运算符。
错误的意思是:int * X
没有 operator*
,X + int
也没有 operator+
。您只有 X * int
、X * X
和 X + X
.
的重载运算符
您可以添加一个转换构造函数并使运算符成为自由函数,然后隐式转换对两个操作数都起作用(注意 getter 应该是 const
):
#include <iostream>
class X {
public:
X(double length=0.0) : length(length) {} // <- converting constructor
double getLength() const { // <- const !!!
return length;
}
void setLength(double len) {
length = len;
}
private:
double length; // Length of a box
};
X operator*(const X& a,const X& b){
return {a.getLength() * b.getLength()};
}
X operator+(const X& a,const X& b){
return {a.getLength() + b.getLength()};
}
int main() {
X ob2{6.0};
X ob3{12.0};
X ob3;
ob1 = ob2 + 2*ob3;
ob1 = ob2*2 + ob3;
ob1 = (ob2 + 2) *ob3;
std::cout << "length of Box : " << ob1.getLength() << std::endl;
}
我正在重载运算符 * 和 + 以从文件中读取一些 class' 变量。我写了下面的代码:
class X {
public:
double getLength(void) {
return length;
}
void setLength( double len ) {
length = len;
}
// Overload + operator to add two Box objects.
X operator*(X temp){
X temp1;
temp1.length = this->length * temp1.length;
return temp1;
}
X operator*(int num){
X temp1;
temp1.length = this->length * num;
return temp1;
}
X operator+(X temp){
X temp1;
temp1.length = this->length + temp1.length;
return temp1;
}
private:
double length; // Length of a box
};
// Main function for the program
int main() {
X ob1; // Declare Box1 of type Box
X ob2; // Declare Box2 of type Box
X ob3; // Declare Box3 of type Box
double result = 0.0; // Store the volume of a box here
ob2.setLength(6.0);
ob3.setLength(12.0);
ob1 = ob2 + 2*ob3;
ob1 = ob2*2 + ob3;
ob1 = (ob2 + 2) *ob3;
cout << "length of Box : " << ob1.getLength() <<endl;
return 0;
}
但是当我尝试编译上面的代码时,出现以下错误:
main.cpp: In function 'int main()':
main.cpp:48:17: error: no match for 'operator*' (operand types are 'int' and 'X')
ob1 = ob2 + 2*ob3;
~^~~~
main.cpp:50:15: error: no match for 'operator+' (operand types are 'X' and 'int')
ob1 = (ob2 + 2) *ob3;
~~~~^~~
main.cpp:27:8: note: candidate: 'X X::operator+(X)'
X operator+(X temp){
我无法理解我的代码中的错误。请帮我解决错误。
当您将运算符作为成员函数时,表达式的左侧是函数调用的对象,右侧是成为参数的对象。
所以根据您的定义:
ob3 * 2:; // Fine: Calls ob3.operator*(2)
2 * ob3; // Not fine, there's no operator* function which takes an int on the left-hand side
你可以使用非成员函数来解决这个问题:
X operator*(int a, X b)
{
return b * a; // Calls b.operator*(a)
}
现在您可以在左侧有一个 int
:
2 * ob3; // Fine: Calls operator+(2, ob3)
我也推荐this operator overloading canonical implementaiton reference。它建议(除其他事项外)您使用 operator*=
,然后根据 *=
实施 operator*
。对于所有二元运算符。
错误的意思是:int * X
没有 operator*
,X + int
也没有 operator+
。您只有 X * int
、X * X
和 X + X
.
您可以添加一个转换构造函数并使运算符成为自由函数,然后隐式转换对两个操作数都起作用(注意 getter 应该是 const
):
#include <iostream>
class X {
public:
X(double length=0.0) : length(length) {} // <- converting constructor
double getLength() const { // <- const !!!
return length;
}
void setLength(double len) {
length = len;
}
private:
double length; // Length of a box
};
X operator*(const X& a,const X& b){
return {a.getLength() * b.getLength()};
}
X operator+(const X& a,const X& b){
return {a.getLength() + b.getLength()};
}
int main() {
X ob2{6.0};
X ob3{12.0};
X ob3;
ob1 = ob2 + 2*ob3;
ob1 = ob2*2 + ob3;
ob1 = (ob2 + 2) *ob3;
std::cout << "length of Box : " << ob1.getLength() << std::endl;
}