首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于+=运算符重载的有关问题

2013-12-26 
关于+运算符重载的问题为啥coutc2+5编译要报错,而写成c2+5coutc2就没错?以下是正确的代码 是c2+

关于+=运算符重载的问题
为啥cout<<c2+=5;编译要报错,而写成c2+=5;cout<<c2;就没错?
以下是正确的代码 是c2+=5;cout<<c2;  就是不知道为啥写成cout<<c2+=5;时编译要报错

#include <iostream.h>
#include <stdlib.h>

int Days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int Days2[14]={0,31,31,28,31,30,31,30,31,31,30,31,30,31};

class CDate
{
public:
CDate(int year=2013,int month=1,int day=1):Year(year),Month(month),Day(day){}
bool IsLeapYear(int year);
void SetTime();
CDate &operator++();
CDate operator++(int);
CDate &operator--();
CDate operator--(int);
CDate &operator+=(int right);
CDate &operator-=(int right);
friend CDate operator+(const CDate &c,int x);
friend CDate operator-(const CDate &c,int x);
friend istream &operator>>(istream &input,CDate &c);
friend ostream &operator<<(ostream &output,const CDate &c);
friend bool operator==(const CDate &c1,const CDate &c2);
friend bool operator!=(const CDate &c1,const CDate &c2);
friend bool operator<(const CDate &c1,const CDate &c2);
friend bool operator>(const CDate &c1,const CDate &c2);
friend bool operator<=(const CDate &c1,const CDate &c2);
friend bool operator>=(const CDate &c1,const CDate &c2);
private:
int Year,Month,Day;
};

bool CDate::IsLeapYear(int year)
{
return ((year%4==0&&year%100!=0)||year%400==0);
}

void CDate::SetTime()
{
cout<<"Please enter a date:";
cin>>Year>>Month>>Day;
if (IsLeapYear(Year))
Days[2]=29;
if (Month<1||Month>12)
{
cout<<"The date is not valid"<<endl;
exit(1);
}
if (Day<1||Day>Days[Month])
{
cout<<"The date is not valid."<<endl;
exit(1);
}
cout<<"Set the date successfully!"<<endl;
}

CDate &CDate::operator++()
{
Day++;
if (IsLeapYear(Year))
Days[2]=29;
else
Days[2]=28;
if (Day>Days[Month])
{
Month++;
Day=1;
if (Month>12)
{
Month=1;
Year++;
}
}
return *this;
}

CDate CDate::operator++(int)
{
CDate old=*this;
++(*this);
return old;
}

CDate &CDate::operator--()
{
Day--;
if (IsLeapYear(Year))
Days[2]=29;
else
Days[2]=28;
if (Day==0)
{
Month--;
if (Month==0)
{
Month=12;
Year--;
}
Day=Days[Month];
}
return *this;
}

CDate CDate::operator--(int)
{
CDate old=*this;
--(*this);
return old;
}

CDate &CDate::operator+=(int right)
{
Day+=right;
while (Day>Days[Month])
{
if (IsLeapYear(Year))
Days[2]=29;
else
Days[2]=28;
Day-=Days[Month];
Month++;
if (Month>12)
{
Month=1;
Year++;
}
}
return *this;
}

CDate &CDate::operator-=(int right)
{
Day-=right;
while (Day<=0)
{
if (IsLeapYear(Year))
Days[2]=29;
else
Days[2]=28;
Day+=Days[Month];
Month--;
if (Month==0)
{
Month=12;
Year--;
}
}
return *this;
}

CDate operator+(const CDate &c,int x)
{
CDate temp=c;
temp.Day+=x;
while (temp.Day>Days[temp.Month])
{
if (temp.IsLeapYear(temp.Year))
Days[2]=29;
else
Days[2]=28;
temp.Day-=Days[temp.Month];
temp.Month++;
if (temp.Month>12)
{
temp.Month=1;
temp.Year++;
}
}
return temp;
}

CDate operator-(const CDate &c,int x)
{
CDate temp=c;
temp.Day-=x;
while (temp.Day<=0)
{
if (temp.IsLeapYear(temp.Year))
Days2[3]=29;
else
Days2[3]=28;
temp.Day+=Days2[temp.Month];
temp.Month--;
if (temp.Month==0)
{
temp.Month=12;
temp.Year--;
}
}
return temp;
}

istream &operator>>(istream &input,CDate &c)
{
input>>c.Year>>c.Month>>c.Day;
return input;
}

ostream &operator<<(ostream &output,const CDate &c)
{
output<<c.Year<<"/"<<c.Month<<"/"<<c.Day<<endl;
return output;
}

bool operator==(const CDate &c1,const CDate &c2)
{
if (c1.Year==c2.Year&&c1.Month==c2.Month&&c1.Day==c2.Day)
return true;
else
return false;
}

bool operator!=(const CDate &c1,const CDate &c2)


{
if (c1.Year!=c2.Year||c1.Month!=c2.Month||c1.Day!=c2.Day)
return true;
else
return false;
}

bool operator<(const CDate &c1,const CDate &c2)
{
if (c1.Year<c2.Year)
return true;
else if (c1.Year>c2.Year)
return false;
else
{
if (c1.Month<c2.Month)
return true;
else if (c1.Month>c2.Month)
return false;
else
{
if (c1.Day<c2.Day)
return true;
else
return false;
}
}
}

bool operator>(const CDate &c1,const CDate &c2)
{
if (c1.Year>c2.Year)
return true;
else if (c1.Year<c2.Year)
return false;
else
{
if (c1.Month>c2.Month)
return true;
else if (c1.Month<c2.Month)
return false;
else
{
if (c1.Day>c2.Day)
return true;
else
return false;
}
}
}

bool operator<=(const CDate &c1,const CDate &c2)
{
if (c1.Year<c2.Year)
return true;
else if (c1.Year>c2.Year)
return false;
else
{
if (c1.Month<c2.Month)
return true;
else if (c1.Month>c2.Month)
return false;
else
{
if (c1.Day<=c2.Day)
return true;
else
return false;
}
}
}

bool operator>=(const CDate &c1,const CDate &c2)
{
if (c1.Year>c2.Year)
return true;
else if (c1.Year<c2.Year)
return false;
else
{
if (c1.Month>c2.Month)
return true;
else if (c1.Month<c2.Month)
return false;
else
{
if (c1.Day>=c2.Day)
return true;
else
return false;
}
}
}

void main()
{
CDate c1,c2;
c1.SetTime();
cout<<c1++;
cout<<++c1;
cout<<c1--;
cout<<--c1;
cin>>c2;
cout<<c2+300;
cout<<c2-300;
c2+=5;
cout<<c2;
c2-=5;
cout<<c2;
if (c1==c2)
cout<<"c1=c2"<<endl;
else
cout<<"c1!=c2"<<endl;
if (c1!=c2)
cout<<"c1!=c2"<<endl;
else
cout<<"c1=c2"<<endl;
if (c1>c2)
cout<<"c1>c2"<<endl;
else
cout<<"c1<=c2"<<endl;
if (c1<c2)
cout<<"c1<c2"<<endl;
else
cout<<"c1>=c2"<<endl;
if (c1>=c2)
cout<<"c1>=c2"<<endl;
else
cout<<"c1<c2"<<endl;
if (c1<=c2)
cout<<"c1<=c2"<<endl;
else
cout<<"c1>c2"<<endl;
}


[解决办法]
+=这个运算符的优先级比较低楼主
[解决办法]
//C++ Operators
//  Operators specify an evaluation to be performed on one of the following:
//    One operand (unary operator)
//    Two operands (binary operator)
//    Three operands (ternary operator)
//  The C++ language includes all C operators and adds several new operators.
//  Table 1.1 lists the operators available in Microsoft C++.
//  Operators follow a strict precedence which defines the evaluation order of
//expressions containing these operators.  Operators associate with either the
//expression on their left or the expression on their right;    this is called
//“associativity.” Operators in the same group have equal precedence and are
//evaluated left to right in an expression unless explicitly forced by a pair of
//parentheses, ( ).
//  Table 1.1 shows the precedence and associativity of C++ operators
//  (from highest to lowest precedence).


//
//Table 1.1   C++ Operator Precedence and Associativity
// The highest precedence level is at the top of the table.
//+------------------+-----------------------------------------+---------------+
//
[解决办法]
 Operator         
[解决办法]
 Name or Meaning                         
[解决办法]
 Associativity 
[解决办法]

//+------------------+-----------------------------------------+---------------+
//
[解决办法]
 ::               
[解决办法]
 Scope resolution                        
[解决办法]
 None          
[解决办法]

//
[解决办法]
 ::               
[解决办法]
 Global                                  
[解决办法]
 None          
[解决办法]

//
[解决办法]
 [ ]              
[解决办法]
 Array subscript                         
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 ( )              
[解决办法]
 Function call                           
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 ( )              
[解决办法]
 Conversion                              
[解决办法]
 None          
[解决办法]

//
[解决办法]
 .                
[解决办法]
 Member selection (object)               
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 ->               
[解决办法]
 Member selection (pointer)              
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 ++               
[解决办法]
 Postfix increment                       
[解决办法]
 None          
[解决办法]

//
[解决办法]
 --               
[解决办法]
 Postfix decrement                       


[解决办法]
 None          
[解决办法]

//
[解决办法]
 new              
[解决办法]
 Allocate object                         
[解决办法]
 None          
[解决办法]

//
[解决办法]
 delete           
[解决办法]
 Deallocate object                       
[解决办法]
 None          
[解决办法]

//
[解决办法]
 delete[ ]        
[解决办法]
 Deallocate object                       
[解决办法]
 None          
[解决办法]

//
[解决办法]
 ++               
[解决办法]
 Prefix increment                        
[解决办法]
 None          
[解决办法]

//
[解决办法]
 --               
[解决办法]
 Prefix decrement                        
[解决办法]
 None          
[解决办法]

//
[解决办法]
 *                
[解决办法]
 Dereference                             
[解决办法]
 None          
[解决办法]

//
[解决办法]
 &                
[解决办法]
 Address-of                              
[解决办法]
 None          
[解决办法]

//
[解决办法]
 +                
[解决办法]
 Unary plus                              
[解决办法]
 None          
[解决办法]

//
[解决办法]
 -                
[解决办法]
 Arithmetic negation (unary)             
[解决办法]
 None          
[解决办法]

//
[解决办法]
 !                
[解决办法]
 Logical NOT                             


[解决办法]
 None          
[解决办法]

//
[解决办法]
 ~                
[解决办法]
 Bitwise complement                      
[解决办法]
 None          
[解决办法]

//
[解决办法]
 sizeof           
[解决办法]
 Size of object                          
[解决办法]
 None          
[解决办法]

//
[解决办法]
 sizeof ( )       
[解决办法]
 Size of type                            
[解决办法]
 None          
[解决办法]

//
[解决办法]
 typeid( )        
[解决办法]
 type name                               
[解决办法]
 None          
[解决办法]

//
[解决办法]
 (type)           
[解决办法]
 Type cast (conversion)                  
[解决办法]
 Right to left 
[解决办法]

//
[解决办法]
 const_cast       
[解决办法]
 Type cast (conversion)                  
[解决办法]
 None          
[解决办法]

//
[解决办法]
 dynamic_cast     
[解决办法]
 Type cast (conversion)                  
[解决办法]
 None          
[解决办法]

//
[解决办法]
 reinterpret_cast 
[解决办法]
 Type cast (conversion)                  
[解决办法]
 None          
[解决办法]

//
[解决办法]
 static_cast      
[解决办法]
 Type cast (conversion)                  
[解决办法]
 None          
[解决办法]

//
[解决办法]
 .*               
[解决办法]
 Apply pointer to class member (objects) 
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 ->*              
[解决办法]
 Dereference pointer to class member     


[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 *                
[解决办法]
 Multiplication                          
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 /                
[解决办法]
 Division                                
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 %                
[解决办法]
 Remainder (modulus)                     
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 +                
[解决办法]
 Addition                                
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 -                
[解决办法]
 Subtraction                             
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 <<               
[解决办法]
 Left shift                              
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 >>               
[解决办法]
 Right shift                             
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 <                
[解决办法]
 Less than                               
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 >                
[解决办法]
 Greater than                            
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 <=               
[解决办法]
 Less than or equal to                   
[解决办法]
 Left to right 


[解决办法]

//
[解决办法]
 >=               
[解决办法]
 Greater than or equal to                
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 ==               
[解决办法]
 Equality                                
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 !=               
[解决办法]
 Inequality                              
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 &                
[解决办法]
 Bitwise AND                             
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 ^                
[解决办法]
 Bitwise exclusive OR                    
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 
[解决办法]
                
[解决办法]
 Bitwise OR                              
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 &&               
[解决办法]
 Logical AND                             
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 
[解决办法]
               
[解决办法]
 Logical OR                              
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 e1?e2:e3         
[解决办法]
 Conditional                             
[解决办法]
 Right to left 
[解决办法]

//
[解决办法]
 =                
[解决办法]
 Assignment                              
[解决办法]
 Right to left 
------解决方案--------------------



//
[解决办法]
 *=               
[解决办法]
 Multiplication assignment               
[解决办法]
 Right to left 
[解决办法]

//
[解决办法]
 /=               
[解决办法]
 Division assignment                     
[解决办法]
 Right to left 
[解决办法]

//
[解决办法]
 %=               
[解决办法]
 Modulus assignment                      
[解决办法]
 Right to left 
[解决办法]

//
[解决办法]
 +=               
[解决办法]
 Addition assignment                     
[解决办法]
 Right to left 
[解决办法]

//
[解决办法]
 -=               
[解决办法]
 Subtraction assignment                  
[解决办法]
 Right to left 
[解决办法]

//
[解决办法]
 <<=              
[解决办法]
 Left-shift assignment                   
[解决办法]
 Right to left 
[解决办法]

//
[解决办法]
 >>=              
[解决办法]
 Right-shift assignment                  
[解决办法]
 Right to left 
[解决办法]

//
[解决办法]
 &=               
[解决办法]
 Bitwise AND assignment                  
[解决办法]
 Right to left 
[解决办法]

//
[解决办法]
 
[解决办法]
=               
[解决办法]
 Bitwise inclusive OR assignment         
[解决办法]
 Right to left 
[解决办法]

//
[解决办法]
 ^=               
[解决办法]
 Bitwise exclusive OR assignment         
[解决办法]
 Right to left 
[解决办法]

//
[解决办法]
 ,                
[解决办法]
 Comma                                   
[解决办法]
 Left to right 
------解决方案--------------------



//+------------------+-----------------------------------------+---------------+


[解决办法]
cout << (c2 += 5);
多简单个事。
其实<<比好多运算符的优先级都高,除了形象,用它来表达流操作真不是什么好选择。

热点排行