codeblocks多文件如何执行呢,
书本上抄的.h头文件代码,.cpp类文件代码,.cpp源文件代码都有了,放在了一个文件夹了,下一步要怎么做呢
参考了网上的调出左侧项目栏的答案,但是没解决
单个程序运行时报错:
C++多文件,类实现文件account.cpp报错undefined reference to "WinMain@16"
主函数文件5_11.cpp报错undefined reference to SavingsAccount::SavingsAccount(int, int, double)
应该不是单个文件的问题,多文件到底怎么运行呢
account.h
#ifndef ACCOUNT_H__
#define ACCOUNT_H__
class SavingsAccount { //储蓄账户类
private:
int id;
double balance;
double rate;
int lastDate;
double accumulation;
static double total;
//记录一笔账,date日期,amount为金额,desc为说明
void record(int date, double amount);
//获得到指定日期为止的存款金额按日累计值
double accumulate(int date) const {
return accumulation+balance*(date-lastDate);
}
public:
//构造函数
SavingsAccount(int date, int id, double rate);
int getId() const{return id;}
double getBalance() const{return balance;}
static double getTotal() {return total;}
void deposit(int date, double amount); //存入现金
void withdraw(int date, double amount); //取出现金
//结算利息,每年1月1日调用一次该函数
void settle(int date);
//显示账户信息
void show() const;
};
#endif // ACCOUNT_H_INCLUDED
account.cpp
#include "account.h"
#include<cmath>
#include<iostream>
using namespace std;
double SavingsAccount::total=0;
//SavingsAccount类相关成员函数的实现
SavingsAccount::SavingsAccount(int date,int id, double rate)
:id(id), balance(0), rate(rate), lastDate(date), accumulation(0) {
cout<<date<<"\t#"<<id<<" is created"<<endl;
}
void SavingsAccount::record(int date, double amount) {
accumulation=accumulate(date);
lastDate=date;
amount=floor(amount*100+0.5)/100;
balance+=amount;
total+=amount;
cout<<date<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<endl;
}
void SavingsAccount::deposit(int date, double amount) {
record(date, amount);
}
void SavingsAccount::withdraw(int date, double amount) {
if(amount > getBalance())
cout<<"Error: not enough money"<<endl;
else
record(date, -amount);
}
void SavingsAccount::settle(int date) {
double interest=accumulate(date) * rate / 365; //计算年息
if(interest != 0)
record(date, interest);
accumulation = 0;
}
void SavingsAccount::show() const {
cout<<"# "<<id<<"\tBalance: "<<balance;
}
5_11.cpp
#include "account.h"
#include<iostream>
using namespace std;
int main()
{
//建立几个账户
SavingsAccount sa0(1, 21325320, 0.015);
SavingsAccount sa1(1, 58320212, 0.015);
//几笔账目
sa0.deposit(5, 5000);
sa1.deposit(25, 10000);
sa0.deposit(45, 5500);
sa1.deposit(60, 4000);
//开户后第90天到了银行计息日,结算所有账户的年息
sa0.settle(90);
sa1.settle(90);
//输出各账户信息
sa0.show(); cout<<endl;
sa1.show(); cout<<endl;
cout<<"Total: "<<SavingsAccount::getTotal()<<endl;
return 0;
}