0

I don't understand why I am getting the following error in my main.cpp file: undefined reference to 'BankDetails::BankDetails()' undefined reference to 'BankDetails::Setname(std::string)' etc etc

I have checked everything and even some references online but they don't explain where I am going wrong. Your help is much appreciated.

BankDetails.h:

#ifndef BANKDETAILS_H
#define BANKDETAILS_H

#include <iostream>
#include <string>

using namespace std;

class BankDetails
{
    public:
        BankDetails();
        void Setname(string n);
        void Setaccnr(string accNr);
        void Settype(string accType);
        void Setbranch(string code);
        void SetinterestRate(double val);
        string Getname() const;
        string Getaccnr()const;
        string Gettype()const;
        string Getbranch()const;
        double GetinterestRate()const;

    private:
        string name;
        string accnr;
        string type;
        string branch;
        double interestRate;
};
#endif // BANKDETAILS_H

BankDetails.cpp:

#include "BankDetails.h"
#include <string>
#include <iostream>

using namespace std;

BankDetails::BankDetails()
{
    name = " ";
    accnr = " ";
    type = " ";
    branch = " ";
    interestRate = 0;
    //ctor
}

void BankDetails::Setname(string n)
{
    name = n;
}

void BankDetails::Setaccnr(string accNr)
{
    accnr = accNr;
}

void BankDetails::Settype(string accType)
{
    type = accType;
}

void BankDetails::Setbranch(string code)
{
    branch = code;
}

void BankDetails::SetinterestRate(double val)
{
    interestRate = val;
}

string BankDetails::Getname()const
{
    return name;
}

string BankDetails::Getaccnr()const
{
    return accnr;
}

string BankDetails::Getbranch()const
{
    return branch;
}

string BankDetails::Gettype()const
{
    return type;
}

double BankDetails::GetinterestRate() const
{
    return interestRate;
}

main.cpp:

#include <iostream>
#include <string>
#include "BankDetails.h"

using namespace std;
//Main function
int main()
{
    BankDetails bankDetails;
    string n, accNr, accType, code;
    double val;

    cout << "Enter account holder name: " ;
    getline(cin, n);
    bankDetails.Setname(n);
    cout << "Enter account number: " ;
    getline(cin, accNr);
    bankDetails.Setaccnr(accNr);
    cout << "Enter account type: " ;
    getline(cin, accType);
    bankDetails.Settype(accType);
    cout << "Enter branch code: " ;
    getline(cin, code);
    bankDetails.Setbranch(code);
    cout << "Enter interest rate: " ;
    cin >> val;
    bankDetails.SetinterestRate(val);

    //Display bank details
    cout <<"\nAccount holder's name: " << bankDetails.Getname() << endl;
    cout <<"Account number: " << bankDetails.Getaccnr() << endl;
    cout <<"Type of account: " << bankDetails.Gettype() << endl;
    cout <<"Branch Code: " << bankDetails.Getbranch() << endl;
    cout <<"Interest rate: " << bankDetails.GetinterestRate() << endl;

    return 0;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
N101Seggwaye
  • 75
  • 2
  • 16
  • 2
    Looks like a linker error. Are you compiling both `.cpp` files? – cigien Apr 13 '20 at 19:06
  • 2
    How did you compile and link? – Cory Kramer Apr 13 '20 at 19:06
  • I'm not sure what you mean by linker error but I will research this. Thank you EDIT: This worked, I had to change my compiler linker settings to include the BankDetails.cpp file and it worked! Thank you @cigien – N101Seggwaye Apr 13 '20 at 19:08
  • Some reading on that: [How does the compilation/linking process work?](https://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work) – user4581301 Apr 13 '20 at 19:10
  • 1
    `BankDetails.cpp` is likely not part of your project. Just putting a cpp file in the project folder does not usually add a file to a project in many IDEs. I am not 100% sure on all the details on Code::Blocks but I do know it has a project file. – drescherjm Apr 13 '20 at 19:10
  • https://stackoverflow.com/a/12574400/103167 – Ben Voigt Apr 13 '20 at 19:11
  • All the things that you are missing are in the file BankDetails.cpp, that should give you a clue. I'm also guessing that this is the first multifile program you've created. It's a hurdle that seems to trip up a lot of newbies. – john Apr 13 '20 at 19:11

0 Answers0