编译器错误 C2804
二进制“operator operator”的参数太多
重载的二进制运算符成员函数被声明为带有多个参数。 暗含了二元运算符成员函数的第一个操作数参数,其类型为该运算符的封闭类型。
示例
下面的示例生成 C2804,并演示如何修复此错误。
// C2804.cpp
// compile by using: cl /c /W4 C2804.cpp
class X {
public:
X& operator+= (const X &left, const X &right); // C2804
X& operator+= (const X &right); // OK - left operand implicitly *this
};
int main() {
X x, y;
x += y; // equivalent to x.operator+=(y)
}
下面的示例生成 C2804,并演示如何修复此错误。
// C2804_2.cpp
// compile with: /clr /c
ref struct Y {
Y^ operator +(Y^ hY, int i); // C2804
static Y^ operator +(Y^ hY, int i); // OK
Y^ operator +(int i); // OK
};
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈