千帐灯无此声 2023-04-21 09:51 采纳率: 100%
浏览 28
已结题

codeblocks多文件项目如何执行

codeblocks多文件如何执行呢,
书本上抄的.h头文件代码,.cpp类文件代码,.cpp源文件代码都有了,放在了一个文件夹了,下一步要怎么做呢
参考了网上的调出左侧项目栏的答案,但是没解决
单个程序运行时报错:
C++多文件,类实现文件account.cpp报错undefined reference to "WinMain@16"
主函数文件5_11.cpp报错undefined reference to SavingsAccount::SavingsAccount(int, int, double)
应该不是单个文件的问题,多文件到底怎么运行呢

img

img


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;
}


  • 写回答

1条回答 默认 最新

  • threenewbee 2023-04-21 10:00
    关注

    参考这个

    关键在于,需要手工添加文件,网上流传的一个include cpp的做法,其实不算多文件
    多文件的意思是分辨编译成obj,再链接

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月29日
  • 已采纳回答 4月21日
  • 创建了问题 4月21日

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持