65,187
社区成员




#include <iostream>
using namespace std;
class Test
{
friend Test operator+(int val, Test const& another);
friend Test operator+(Test const& another,int val);
friend ostream& operator<<(ostream& os, const Test& obj);
private:
int m_val;
public:
Test()
{
m_val=10;
}
public:
};
Test operator+(int val, Test const& another)
{
Test obj;
obj.m_val=val+another.m_val;
return obj;
}
Test operator+(Test const& another,int val)
{
Test obj;
obj.m_val=val+another.m_val;
return obj;
}
ostream& operator<<(ostream& os, const Test& obj)
{
cout << obj.m_val;
return os;
}
int main()
{
Test obj;
Test obj2;
obj2=2+obj;
cout << obj2 << endl;
return 0;
}
class Test
{
private:
int m_val;
public:
Test()
{
m_val=10;
}
public:
Test& operator+(int val, Test const& another)
{
m_val=val+another.m_val;
return *this;
}
Test& operator+(Test const& another,int val)
{
m_val=val+another.m_val;
return *this;
}
};
int main()
{
Test obj;
Test obj2;
obj2=2+obj;
return 0;
}
提示错误
error C2804: 二进制“operator +”的参数太多
template< class CharT, class Traits, class Alloc >
basic_string<CharT,Traits,Alloc>
operator+( const CharT* lhs,
const basic_string<CharT,Traits,Alloc>& rhs );
template< class CharT, class Traits, class Alloc >
basic_string<CharT,Traits,Alloc>
operator+( const basic_string<CharT,Traits,Alloc>& lhs,
const CharT* rhs );