0

I am attempting to make part of a program that uses a bank account class as the base class and checking and savings as the derived classes. I have been trying to set up the basic framework before I do any fancy data handling and I've followed some tutorials to get a better understanding of classes and inheritance.

I have looked for answers but the answers I have found don't seem to be my problem but I might just need another set of eyes on my code.

the compiler errors:

In function main': badriver.cpp:20: undefined reference toChecking::getAccount()' badriver.cpp:23: undefined reference to Checking::setAccount(int)' badriver.cpp:24: undefined reference toSavings::setAccount(int)' badriver.cpp:26: undefined reference to `Checking::getAccount()'

badriver.cpp

#include "BankAccount.cpp"
#include "Checking.cpp"
#include "Savings.cpp"
#include <string>
#include <iostream>
using namespace std;

int main(){

   Checking c;
   Savings s;

   cout << "Checking: " << c.getAccount() << " - Type: " << c.getType() << endl;
   cout << "Savings: " << s.getAccount() << " - Type: " << s.getType() << endl;

   c.setAccount(9);
   s.setAccount(15);

   cout << "New Checking: " << c.getAccount() << endl;
   cout << "New Savings: " << s.getAccount() << endl;
       return 0;
    }

BankAccount.h

#ifndef BANKACCOUNT_H
#define BANKACCOUNT_H
#include <string>
using std::string;
using std::ostream;
using std::istream;

class BankAccount{

private:
   int myAccount;

   const char* color;

public:
   // default constructor
   BankAccount();
   BankAccount(int account);
   virtual ~BankAccount();

   virtual void setAccount(int)=0;
   int getAccount();
//    
//    void setSAccount(int);
//    int getSAccount();
//    
   virtual const char* getColor();                                            
   virtual const char* getType() = 0; 
   //virtual const char* getCType() = 0; 

protected:
   void setColor(const char*);   

};

#endif // BANKACCOUNT_H

BankAccount.cpp

#include "BankAccount.h"
#include "Checking.h"
#include "Savings.h"
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;

// default constructor
BankAccount::BankAccount(){
   account = 1;
}

BankAccount::~BankAccount(){}

// void BankAccount::setAccount(int account){
//    myAccount  = account;
// }

int BankAccount::getAccount(){
   return myAccount ;
}

BankAccount::BankAccount(int account){
   myAccount = account;
}

const char* BankAccount::getColor(){
   return color;
}

void BankAccount::setColor(const char* c){
   color = c;
}

Checking.h

#ifndef CHECKING_H
#define CHECKING_H
#include "BankAccount.h"
#include <string>
using std::string;
using std::ostream;
using std::istream;

class Checking : public BankAccount{

private:
   const char* type;

public:
   Checking();
   virtual ~Checking();
   void setAccount(int account);


   virtual const char* getType();

   void setChecking(int);
   int getChecking();
};

#endif //CHECKING_H

Checking.cpp

#include "Checking.h"
#include <string>
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;

Checking::Checking() : BankAccount(1), type("Checking"){}

Checking::~Checking(){}

BankAccount::~BankAccount(){}

void BankAccount::setAccount(int account){
   myAccount  = account;
}


const char* Checking::getType(){
   return type;
}   

Savings.h

#ifndef  SAVINGS_H
#define SAVINGS_H
#include "BankAccount.h"
#include <string>
using std::string;
using std::ostream;
using std::istream;

class Savings: public BankAccount{

private:
   const char* type;

public:
   Savings();
   virtual ~Savings();
   void setAccount(int account);

   virtual const char* getType();

   void setSavings(int);
   int getSavings();
};

#endif // SAVINGS_H

Savings.cpp

#include "Savings.h"
#include <string>
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;

Savings::Savings() : BankAccount(2), type("Savings"){}

Savings::~Savings(){}

BankAccount::~BankAccount(){}

void BankAccount::setAccount(int account){
   myAccount  = account;
}

const char* Savings::getType(){
   return type;
}   

Thanks for any help pointing me in the right direction.

6
  • 7
    Those are linker errors. Most likely, Checking.cpp and Savings.cpp are not actually being built or linked with. Make sure they are part of your build. Commented Jul 27, 2014 at 20:36
  • You might want to add the virtual keyword to the definitions of setAcccount() in Checking and Savings. Commented Jul 27, 2014 at 20:40
  • I changed the #include "class.h" files to #include "class.cpp" files and that seemed to fix the linker errors. I also tried adding the virtual keyword to setAccount() and I still get the following error messages: In function main': badriver.cpp:20: undefined reference to Checking::getAccount()' badriver.cpp:23: undefined reference to Checking::setAccount(int)' badriver.cpp:24: undefined reference to Savings::setAccount(int)' badriver.cpp:26: undefined reference to `Checking::getAccount()' Commented Jul 27, 2014 at 21:03
  • Including the definitions (in the cpp file) in the header file is not going to work if you want to include that header in more than one compilation unit. Commented Jul 27, 2014 at 21:26
  • What IDE are you using? How are you compiling your project? Commented Jul 27, 2014 at 21:28

1 Answer 1

2

Checking.cpp and Savings.cpp contain:

BankAccount::~BankAccount(){}

void BankAccount::setAccount(int account){
     myAccount  = account;
}

This causes undefined behaviour because you defined those functions in multiple files. You need to delete those lines from Checking.cpp and Savings.cpp, and instead put in definitions for the functions which are listed as being missing in the compiler output:

void Checking::setAccount(int account){
     // code here
}

etc.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. Sometimes the little things are hard to see!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.