[英]Why can't a class member function take more than one argument of the same type as its class
The other day, I ran into trouble when I tried to overload a two-parameter operator with the use of a class member function. 前几天,当我尝试使用类成员函数重载两个参数的运算符时,我遇到了麻烦。 I tried references, but nothing changed. 我尝试了参考,但没有改变。 The compiler said I couldn't write a member function that takes more than one argument of the same type as the class itself. 编译器说我无法编写一个成员函数,该成员函数需要多个与类本身相同类型的参数。 Why is that? 这是为什么?
Here is the code: 这是代码:
class Fraction
{
public:
Fraction(int num=1, int den=1): numerator(num), denominator(den) {}
Fraction(const Fraction& r): numerator(r.numerator), denominator(r.denominator) {}
Fraction& operator=(const Fraction&);
Fraction& operator*(const Fraction&, const Fraction&);
private:
int numerator, denominator;
};
Fraction& Fraction::operator=(const Fraction& r)
{
numerator = r.numerator;
denominator = r.denominator;
return *this;
}
Fraction Fraction::operator*(const Fraction& x, const Fraction& y)
{
Fraction z(x.numerator*y.numerator, x.denominator*y.denominator);
return z;
}
The following is the error message from the compiler: 以下是来自编译器的错误消息:
Fraction& Fraction::operator*(const Fraction&, const Fraction&)' must take either zero or one argument
The operators take a fixed amount of arguments, for example operator+ as the addition operator takes exactly 2 parameters. 运算符采用固定数量的参数,例如operator +,因为加法运算符恰好采用2个参数。 If it is a member function the first(leftmost) is implied to be this and the second is passed as a parameter. 如果它是一个成员函数的第一个(最左边)隐含为此 ,第二个是作为参数传递。
What would calling an operator+ that took three parameters look like? 调用带有三个参数的operator +会是什么样子? One might imagine it would look like 3 + 4 + 5 but that is the equivalent of calling operator+(3,4) then operator+(7,5). 一个人可能会想象它看起来像3 + 4 + 5,但这相当于调用operator +(3,4)然后调用operator +(7,5)。
For a list of operators and how many arguments they take please check out wikipedia: http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B 有关运算符及其使用的参数的列表,请查看Wikipedia: http : //en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B
For more details there is another StackOverflow post that goes into great detail: Operator overloading 有关更多详细信息,还有另一个StackOverflow帖子,其中有很详细的内容: 操作符重载
Firstly, the issue you are observing has absolutely nothing to do with member functions in general. 首先,您所观察到的问题通常与成员函数完全无关。 Member functions can generally take arbitrary number of arguments of "same class" type. 成员函数通常可以接受任意数量的“相同类”类型的参数。 In your example you can declare 在您的示例中,您可以声明
class Fraction
{
void foo(Fraction &f1, Fraction &f2, Fraction &f3, Fraction &f4) {}
...
};
without any problems. 没有任何问题。 So, it is not clear why you decided to word your question as if it is about member functions in general. 因此,不清楚您为什么决定将您的问题用措辞笼统地视为关于成员函数。
Secondly, in your code it is really about the simple fact that you are trying to overload an operator . 其次,在您的代码中,这实际上与您试图使运算符重载有关的简单事实有关。 Operator syntax in C++ is fixed for most (but not all) operators. 对于大多数(但不是全部)运算符,C ++中的运算符语法是固定的。 This immediately means that those operators whose syntax is fixed will have fixed number of parameters. 这立即意味着那些语法固定的运算符将具有固定数量的参数。
In your example, it is operator *
. 在您的示例中,它是运算符*
。 It can be unary (one parameter) or binary (two parameters). 它可以是一元(一个参数)或二进制(两个参数)。 When you overload this operator by a member function one parameter is already implied, so you can only add zero or one additional parameters (for unary *
and binary *
respectively). 当通过成员函数重载该运算符时,已经隐含了一个参数,因此只能添加零个或一个附加参数(分别用于一元*
和二进制*
)。 You, on the other hand, are trying to add two more parameters in additions to the implicit one. 另一方面,您正在尝试在隐式参数中添加另外两个参数。 Ie you are trying to define a ternary operator *
. 也就是说,您正在尝试定义三元运算符*
。 This is not possible. 这是不可能的。 There's no ternary *
operator in C++. C ++中没有三元*
运算符。 And this is exactly what the compiler is telling you. 这正是编译器告诉您的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.