Home > Back-end >  Undefined symbols: "BankAccount::BankAccount( ... "
Undefined symbols: "BankAccount::BankAccount( ... "

Time:03-01

i'm new to c and am learning from a course. I trying to write a program using classes to store bank info. This is how my class is set up.

#include <iostream>
using namespace std;

class BankAccount {

private:
    double balance = 0;
    int acountNumber = 0;
    string ownerName;
    double interestRate = 0;

public:
    
    double getBalance()
    {
        return balance;
    }
    void deposit(double userINPUT)
    {
        balance = balance   userINPUT;
    }
    void withdraw(double userINPUT)
    {
        balance = balance - userINPUT;
    }
    void addInterest()
    {
        balance = balance * (1   interestRate);
    }
    
    BankAccount();
    BankAccount(double balance, double interestRate, int acountNumber, string ownerName);

    void displayAccountSummary()
    {
        cout << "Account Number : " << acountNumber;
        cout << "\nOwner’s Name : " << ownerName;
        cout << "\nBalance : " << balance;
        cout << "\nInterest rate : " << interestRate << "%" <<endl;
    }
};

i am trying to run the following commands into my main(), but one the first line -

BankAccount myAcount(1000.50, 0.05, 1111, "John Williams"); 

..I get the error that I don't understand.

Undefined symbols: "BankAccount::BankAccount(double, double, int, std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator >)

this is what I'm trying to call

BankAccount myAcount(1000.50, 0.05, 1111, "John Williams"); //error here
myAcount.deposit(500); 
myAcount.withdraw(200);
myAcount.addInterest();
myAcount.displayAccountSummary();
return 0;

any idea what I need to fix?

CodePudding user response:

Implement your constructors as such:

BankAccount() {}
BankAccount(double balance, double interestRate, int acountNumber, string ownerName) 
    : balance(balance), interestRate(interestRate), acountNumber(acountNumber), ownerName(ownerName) {}

Using an unimplemented function causes linker issues.

CodePudding user response:

All of your BankAccount methods are being implemented inline in the class declaration itself, except for the two constructors. So their implementations must exist outside of the class declaration.

The linker is complaining that it can't find the implementation for the constructor that you are actually calling. You need to include that implementation in your code somewhere, eg:

class BankAccount {
...
public:
    ...

    BankAccount();
    // or:
    // BankAccount() = default;

    BankAccount(double balance, double interestRate, int acountNumber, string ownerName);

    ...
};
// if not default'ed...
BankAccount::BankAccount()
{
}

BankAccount::BankAccount(double balance, double interestRate, int acountNumber, string ownerName)
    : balance(balance), interestRate(interestRate), acountNumber(acountNumber), ownerName(ownerName)
{
} 

If those implementations are in a separate source file, make sure you are compiling and linking that file.

CodePudding user response:

The problem is that you've only declared the default constructor and the parameterized constructor and not defined them.

So when you use the constructor by writing BankAccount myAcount(1000.50, 0.05, 1111, "John Williams");, the linker cannot find the definition for the parameterized constructor since you've only declared that constructor and not defined it.

To solve this problem you need to define the constructors as shown below:

class BankAccount {

    private:
    //other members here as before
    public:
    
   
    //define default constructor
    BankAccount()
    {
    }
    //define parameterized constructor and use constructor initializer list
    BankAccount(double balance, double interestRate, int acountNumber, string ownerName) 
    : balance(balance), interestRate(interestRate), acountNumber(acountNumber), ownerName(ownerName) 
    {
    }  
    //other code as before
};

Here we have used constructor initializer list in the parameterized constructor to initialize each of the data members using the constructor parameters.

  •  Tags:  
  • c
  • Related