C++编程问题

#include<iostream>
using namespace std;

class Money
{
public:
Money operator +(Money& amount1,Money& amount2);

void write();

Money(long doallars,int cents);

int get_write();
private:
long all_cents;
};

int main()
{
Money cost(1,50),tax(0,15),total;
total=cost+tax;
total.write();
return 0;
}

Money::Money(long dollars,int cents)
{
all_cents=100*dollars+cents;
}
Money operator +(Money& amount1,Money& amount2)
{
Money temp;

temp.get_write()=amount1.get_write()+amount2.get_write();
return temp;
}
void Money::write()
{
cout<<all_cents;
}
int Money::get_write()
{
return all_cents;
}
下面是错误信息
D:\Microsoft Visual Studio\MyProjects\试错题\lihui.cpp(7) : error C2804: binary 'operator +' has too many parameters
D:\Microsoft Visual Studio\MyProjects\试错题\lihui.cpp(20) : error C2512: 'Money' : no appropriate default constructor available
D:\Microsoft Visual Studio\MyProjects\试错题\lihui.cpp(21) : error C2676: binary '+' : 'class Money' does not define this operator or a conversion to a type acceptable to the predefined operator
D:\Microsoft Visual Studio\MyProjects\试错题\lihui.cpp(33) : error C2512: 'Money' : no appropriate default constructor available
D:\Microsoft Visual Studio\MyProjects\试错题\lihui.cpp(35) : error C2106: '=' : left operand must be l-value

哪个高手能一下解决掉我在加30分

第1个回答  2008-09-04
#include<iostream>
using namespace std;

class Money
{
public:
friend Money operator +(Money& amount1,Money& amount2);

void write();

Money(long cents = 0L);
Money(long doallars,long cents);

long get_write();
private:
long all_cents;
};

int main()
{
Money cost(1L,50L),tax(0L,15L),total;
total=cost+tax;
total.write();
return 0;
}

Money::Money(long cents)
{
all_cents = cents;
}

Money::Money(long dollars,long cents)
{
all_cents=100*dollars+cents;
}
Money operator +(Money& amount1,Money& amount2)
{
Money temp(amount1.get_write()+amount2.get_write());

return temp;
}
void Money::write()
{
cout<<all_cents;
}
long Money::get_write()
{
return all_cents;
}
第2个回答  2008-09-04
#include<iostream>
using namespace std;

class Money
{
public:
Money & operator +(Money& amount);

void write();

Money(long doallars=0,int cents=0);

int get_write();
private:
long all_cents;
};

int main()
{
Money cost(1,50),tax(0,15),total;
total=cost+tax;
total.write();
return 0;
}

Money::Money(long dollars,int cents)
{
all_cents=100*dollars+cents;
}
Money & Money:: operator +(Money& amount)
{
Money temp;

//temp.get_write()=get_write()+amount.get_write();
temp.all_cents=all_cents+amount.all_cents;
return temp;
}
void Money::write()
{
cout<<all_cents;
}
int Money::get_write()
{
return all_cents;
}本回答被网友采纳
相似回答
大家正在搜