关于运算符重载
编译环境WinXP + VC6
class CInt
{
public:
void setValue(unsigned int v);
unsigned int getValue() const;
CInt();
CInt(unsigned int v);
virtual ~CInt();
void operator=(unsigned int v);
CInt& operator=(CInt&v);
CInt& operator*(const CInt& v1,const CInt& v2);
CInt& operator+(CInt&v1,CInt&v2);
CInt& operator-(CInt&v1,CInt&v2);
CInt& operator*(CInt&v1, unsigned int v2);
CInt& operator+(CInt&v1, unsigned int v2);
CInt& operator-(CInt&v1, unsigned int v2);
CInt& operator*=(CInt&v1);
CInt& operator+=(CInt&v1);
CInt& operator-=(CInt&v1);
private:
unsigned int m_value; // indicate the value
};
错误:
Compiling...
Int.cpp
\int.h(22) : error C2804: binary 'operator *' has too many parameters
\int.h(23) : error C2804: binary 'operator +' has too many parameters
\int.h(24) : error C2804: binary 'operator -' has too many parameters
\int.h(25) : error C2804: binary 'operator *' has too many parameters
\int.h(26) : error C2804: binary 'operator +' has too many parameters
\int.h(27) : error C2804: binary 'operator -' has too many parameters
Error executing cl.exe.
class CInt
{
public:
void setValue(unsigned int v);
unsigned int getValue() const;
CInt();
CInt(unsigned int v);
virtual ~CInt();
void operator=(unsigned int v);
CInt& operator=(CInt&v);
CInt& operator*(const CInt& v1,const CInt& v2);
CInt& operator+(CInt&v1,CInt&v2);
CInt& operator-(CInt&v1,CInt&v2);
CInt& operator*(CInt&v1, unsigned int v2);
CInt& operator+(CInt&v1, unsigned int v2);
CInt& operator-(CInt&v1, unsigned int v2);
CInt& operator*=(CInt&v1);
CInt& operator+=(CInt&v1);
CInt& operator-=(CInt&v1);
private:
unsigned int m_value; // indicate the value
};
错误:
Compiling...
Int.cpp
\int.h(22) : error C2804: binary 'operator *' has too many parameters
\int.h(23) : error C2804: binary 'operator +' has too many parameters
\int.h(24) : error C2804: binary 'operator -' has too many parameters
\int.h(25) : error C2804: binary 'operator *' has too many parameters
\int.h(26) : error C2804: binary 'operator +' has too many parameters
\int.h(27) : error C2804: binary 'operator -' has too many parameters
Error executing cl.exe.
...全文
相关推荐
C++ 重载运算符和重载函数
"C++ 重载运算符和重载函数"C++ 语言的一个关键特性是函数重载和运算符重载,这使得程序员能够根据需要定制和扩展语言的功能。函数重载允许在同一个作用域内声明多个同名函数,但每个函数的参数列表必
详解C++ 重载运算符和重载函数
C++ 重载运算符和重载函数是编程中的一种高级特性,它允许开发者在同一个作用域内为相同名称的操作提供多种实现方式,从而增加代码的灵活性和表达力。这两种重载主要涉及到函数和运算符的多态性应用。1.
详解C++编程中的单目运算符重载与双目运算符重载
"这篇资源详细介绍了C++编程中关于单目运算符和双目运算符的重载技术,特别是以自增运算符`++`为例,展示了如何在类中实现单目运算符重载,同时引入了后置自增运算符的重载方法。"在C++中,运算符
C++重载运算符的规则详解
C++重载运算符的规则详解主要涉及对C++编程语言中运算符重载机制的深入探讨。C++中,虽然允许程序员对已有的运算符进行重载,但并非所有的运算符都可以随意修改。以下几点是重载运算符时需注意的关键规则:
C++运算符重载规则详解
首先,C++中大部分运算符都可以被重载,包括算术运算符、比较运算符、逻辑运算符等。但有五个运算符不允许被重载:成员访问运算符`.`、成员指针访问运算符`.


