0

I am getting undefined reference errors when I try to debug the following code.I tried simple test piece but still the same errors.First I thought it was c++ class creator but after than I wrote the Account.hpp and Account.cpp manualy still the same.How can I fix it.

Error Message:Terminal

main.cpp

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

using namespace std;

int main()
{
    Account erol_account;
    erol_account.set_name("Erol's Account");
    erol_account.set_balance(1000.0);

    if (erol_account.deposit(200.0))
    {
        cout << "Deposit OK\n";
    }
    else
    {
        cout << "Deposit Not Allowed\n";
    }

    if (erol_account.withdraw(500.0))
    {
        cout << "Withdrawal OK\n";
    }
    else
    {
        cout << "No Sufficent Funds\n";
    }

    if (erol_account.withdraw(1500.0))
    {
        cout << "Withdrawal OK\n";
    }
    else
    {
        cout << "No Sufficent Funds\n";
    }

    return 0;
}

Account.hpp


    #ifndef _ACCOUNT_H_
#define _ACCOUNT_H_
#include <string>

class Account
{
private:
    // Attributes

    std::string name;
    double balance;

public:
    // Methods
    // Declared Inline
    void set_balance(double bal) { balance = bal; }
    double get_balance() { return balance; }

    // Methods will declared outside the class decleration
    void set_name(std::string n);
    std::string get_name();

    bool deposit(double amount);
    bool withdraw(double amount);
};

#endif

Account.cpp

#include "Account.hpp"

void Account::set_name(std::string n)
{
    name = n;
}
std::string Account::get_name()
{
    return name;
}
bool Account::deposit(double amount)
{
    // If very amount
    balance += amount;
    return true;
}

bool Account::withdraw(double amount)
{
    if (balance - amount >= 0)
    {
        balance -= amount;
        return true;
    }
    else
    {
        return false;
    }
}
zeNnG
  • 1
  • 1
  • 1
    My first guess is that you aren't compiling all of your *.cpp files. – sweenish Oct 20 '21 at 18:45
  • If you are using VSCode in the default mode it will only build the active file into your executable. The documentation explains the change you need to make to use more than 1 source file: [https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson](https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson) – drescherjm Oct 20 '21 at 20:33
  • @drescherjm i added "${workspaceFolder}\\*.cpp" to the task.json but its still the same. – zeNnG Oct 21 '21 at 19:39
  • Look at the build command in the terminal to see what it is doing. Your question probably would not have been closed so soon if you added your `tasks.json` and build output to your question as text. When questions about linker errors lack information to debug the situation they usually get closed quickly as this duplicate because there is very little we can do to help without the information. – drescherjm Oct 21 '21 at 19:44
  • You probably also need to add the exact text of the error messages as text. Avoid adding pictures of text at StackOverflow. – drescherjm Oct 21 '21 at 19:49
  • @drescherjm should i open new question ?And build command is only compling the main. – zeNnG Oct 21 '21 at 19:52
  • @drescherjm i opened new question its here: https://stackoverflow.com/questions/69668479/getting-errors-while-compling-multiple-c-files – zeNnG Oct 21 '21 at 20:36

0 Answers0