菜鸟求助,error LNK2001

u010235951 2013-05-26 09:04:52

# include <iostream>
# include <string>
using namespace std;

class Account
{
private:
string name;
double balance;
static double rate;
public:
Account(string n,double b): name(n), balance(b){}
Account();
~Account() {}
Account(const Account& ac)
{}
Account& operator = (const Account& ac);
void Display () const;
double passyear(int);
static void SetRate(double);

};

Account& Account:: operator = (const Account& ac)
{
name = ac.name;
balance = ac.balance;
rate = ac.rate;

return *this;
}
void Account::Display() const
{
cout<<"name "<<name<<" balance "<<balance<<endl;
}
void Account::SetRate(double r)
{
rate = r;
}
double Account:: passyear(int)
{
balance = (1 + rate) * balance;
return balance;
}

int main()
{
Account::SetRate(0.35); //设置年利率
Account ac1("ligang",1000.0); //创建账户名ligang, 余额为1000.0的帐户
Account ac2(ac1);
Account ac3;
ac3=ac1=ac2;
ac3.passyear(1); //计算1年后的余额
ac1.Display(); //显示账户名及余额
ac2.Display();
ac3.Display();
return 0;
}

考试1.obj : error LNK2001: unresolved external symbol "public: __thiscall Account::Account(void)" (??0Account@@QAE@XZ)
链接时报错 QAQ
...全文
66 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
diaolingle 2013-05-26
  • 打赏
  • 举报
回复
#include "stdafx.h"
# include <iostream>
# include <string>
using namespace std;

class Account
{
private:
string name;
double balance;
static double rate;
public:
Account(string n,double b): name(n), balance(b){}
Account(){}
~Account() {}
Account(const Account& ac):name(ac.name),balance(ac.balance){}
Account& operator = (const Account& ac);
void Display () const;
double passyear(int);
 static void SetRate(double);

};

Account& Account:: operator = (const Account& ac)
{
name  = ac.name;
balance = ac.balance;
rate = ac.rate;

return *this;
}
void Account::Display() const
{
cout<<"name "<<name<<" balance "<<balance<<endl;
}
void Account::SetRate(double r)
{
 rate = r;
}
double Account:: passyear(int)
{
balance = (1 + rate) * balance;
return balance;
}

double Account::rate=0;
int main()
{
   Account::SetRate(0.35);   //设置年利率
   Account ac1("ligang",1000.0); //创建账户名ligang, 余额为1000.0的帐户
   Account ac2(ac1);
   Account ac3;
   ac3=ac1=ac2;
   ac3.passyear(1);          //计算1年后的余额
   ac1.Display();            //显示账户名及余额
   ac2.Display();
   ac3.Display();
   return 0;
}
正确代码如上. 原代码有几个错误 1.声明默认构造函数但未定义 2.静态成员未在类外事先初始化 3.复制构造函数无初始化列表,而你又ac3=ac1=ac2造成无意义的实现
buyong 2013-05-26
  • 打赏
  • 举报
回复
class Account { private: string name; double balance; static double rate; public: Account(string n,double b): name(n), balance(b){} Account(){} ~Account() {} Account(const Account& ac) {} Account& operator = (const Account& ac); void Display () const; double passyear(int); static void SetRate(double); };
窗外蓝天 2013-05-26
  • 打赏
  • 举报
回复
构造函数没实现
hugett 2013-05-26
  • 打赏
  • 举报
回复

# include <iostream>
# include <string>
using namespace std;

class Account
{
private:
	string name;
	double balance;
	static double rate;
public:
	Account(string n,double b): name(n), balance(b){}
	Account(){}//这里没有定义。。
	~Account() {}
	Account(const Account& ac){}
	Account& operator = (const Account& ac);
	void Display () const;
	double passyear(int);
	static void SetRate(double);
};

double Account::rate;//static成员要类外定义

Account& Account:: operator = (const Account& ac)
{
	name  = ac.name;
	balance = ac.balance;
	rate = ac.rate;

	return *this;
}
void Account::Display() const
{
	cout<<"name "<<name<<" balance "<<balance<<endl;
}
void Account::SetRate(double r)
{
	rate = r;
}
double Account:: passyear(int)
{
	balance = (1 + rate) * balance;
	return balance;
}

int main()
{
   Account::SetRate(0.35);   //设置年利率
   Account ac1("ligang",1000.0); //创建账户名ligang, 余额为1000.0的帐户
   Account ac2(ac1);
   Account ac3;
   ac3=ac1=ac2;
   ac3.passyear(1);          //计算1年后的余额
   ac1.Display();            //显示账户名及余额
   ac2.Display();
   ac3.Display();
   return 0;
}

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧