【问题标题】:undefined reference to `Account::set_balance(double)' ----What does it mean? [duplicate]undefined reference to `Account::set_balance(double)' ----这是什么意思? [复制]
【发布时间】:2021-11-05 20:36:14
【问题描述】:

Account.h 文件

#ifndef ACCOUNT_H
#define ACCOUNT_H

class Account
{
private:
    //attributes
    double balance;
public:
    //methods
    void set_balance(double bal);
    double get_balance();
};

#endif

Account.cpp 文件

#include "Account.h"
void Account::set_balance(double bal){
    balance=bal;
}
double Account::get_balance(){
    return balance;
}

ma​​in.cpp 文件

#include<iostream>
#include"Account.h"

int main(){
    Account frank_account;
    frank_account.set_balance(200.00);
    double showBalance=frank_account.get_balance();
    return 0;
}

错误

C:\Users\Dell\AppData\Local\Temp\ccZOi0ua.o: In function `main':
F:/OPP/opp10/main.cpp:6: undefined reference to `Account::set_balance(double)'
F:/OPP/opp10/main.cpp:7: undefined reference to `Account::get_balance()'
collect2.exe: error: ld returned 1 exit status

Build finished with error(s).
The terminal process terminated with exit code: -1.

请解释错误信息并提供解决方案。 谢谢................................................ ..................................................... .....

【问题讨论】:

  • 构建程序通常有两个主要步骤:将源代码编译成对象,然后将这些对象链接成可执行文件。您的错误消息表明您没有向链接器提供所有对象,或者顺序错误。产生此错误的命令是什么?

标签: c++ class object oop


【解决方案1】:

你是如何编译这两个文件的?此错误表明您未编译或链接“Account.cpp”。试试这个:

gcc Account.cpp main.cpp

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-12
    • 1970-01-01
    • 2018-06-21
    • 2012-10-26
    相关资源
    最近更新 更多