-1

I am trying to practice object oriented programming structure in c++. I have written some piece of code. But I got an undefined reference error.

I've tried to build with clion ide but it gave me error. I've tried to compile it on linux terminal using g++ command but it gave me same error.

Account.h

#ifndef ACCOUNT_H
#define ACCOUNT_H

class Account
{
public:
    Account ();
    Account(double init_balance);
    double getBalance();
    void deposit(double amt);
    void withdraw(double amt);
private:
    double balance;
};

#endif // ACCOUNT_H

Account.cpp

#include "Account.h"
#include <iostream>
using namespace std;

Account::Account(double init_balance)
:balance(init_balance)
{
}

double Account::getBalance(){
    return balance;
}

void Account::deposit(double amt){
    balance += amt;
}

void Account::withdraw(double amt){
    balance -= amt;
}

Customer.h

#ifndef CUSTOMER_H
#define CUSTOMER_H
#include "Account.h"
#include <iostream>
#include <string>

using namespace std;

class Customer
{
public:
    Customer(string first_Name, string last_Name) :
    firstName(first_Name), lastName(last_Name)
    { 
    }
    //Customer(string first_Name, string last_Name);
    string getFirstName();
    string getLastName();
    Account getAccount();
    void setAccount(Account acc);

private:
    string firstName;
    string lastName;
    Account account;
};

#endif // CUSTOMER_H

Customer.cpp

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

using namespace std;


string Customer::getFirstName(){
    return firstName;
}

string Customer::getLastName(){
    return lastName;
}

Account Customer::getAccount(){
    return account;
}

void Customer::setAccount(Account acc)
{
    account = acc;
}

main.cpp

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

using namespace std;

int main(int argc, char** argv) {
Customer *customer;
Account account(0.0);

// Create an account that can has a 500.00 balance.
cout << endl << "Creating the customer Jane Smith.";
customer = new Customer("Jane", "Smith");
cout << endl << "Creating her account with a 500.00 balance.";
customer->setAccount(Account(500.00));
account = customer->getAccount();

cout << endl << "Withdraw 150.00";
account.withdraw(150.00);

cout << endl << "Deposit 22.50";
account.deposit(22.50);

cout << endl << "Withdraw 47.62";
account.withdraw(47.62);

// Print out the final account balance
cout  << endl 
      << "Customer [" 
      << customer->getLastName()
  << ", " 
      << customer->getFirstName()
  << "] has a balance of " 
      << account.getBalance() 
      << endl;     
delete customer;

Account acc1(100), acc2(200);

double suAcc = acc1.getBalance()+ acc2.getBalance();
Account sumAcc(suAcc);

cout << "Balance of sumAcc is " << sumAcc.getBalance() << endl;
return (EXIT_SUCCESS);
}

Here is the error:

/cygdrive/c/Users/James/CLionProjects/untitled13/main.cpp:9: undefined reference to `Account::Account(double)'

/cygdrive/c/Users/James/CLionProjects/untitled13/main.cpp:9:(.text+0x2f): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Account::Account(double)'

/cygdrive/c/Users/James/CLionProjects/untitled13/main.cpp:15: undefined reference to `Account::Account(double)'

/cygdrive/c/Users/James/CLionProjects/untitled13/main.cpp:15:(.text+0x12e): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Account::Account(double)'

/cygdrive/c/Users/James/CLionProjects/untitled13/main.cpp:15: undefined reference to `Customer::setAccount(Account)

'

It goes on with every method.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
James
  • 25
  • 5
  • 2
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Alan Birtles Sep 07 '19 at 08:34
  • presumably you are only compiling `main.cpp`, you need to compile all your `cpp` files and link them together – Alan Birtles Sep 07 '19 at 08:35
  • Thanks for the answer. I wonder if I do it manually all the time or does IDE's do it automaticly? – James Sep 07 '19 at 08:38
  • most IDEs (including CLion I think?) don't build code they just invoke build tools like make or cmake, which one are you using? – Alan Birtles Sep 07 '19 at 08:41
  • I am using clion and I do manually edit cmakelist.txt – James Sep 07 '19 at 08:44
  • 1
    `Account::Account ()` is declared but not defined. This constructor is used when you instantiate a `Customer` which has a member called `account`. This member is constructed using the default constructor. A typical definition could be `Account::Account() : balance(0) {}`. – frogatto Sep 07 '19 at 09:16
  • That is the answer from the dupe covering your case: https://stackoverflow.com/a/12574403/5769463 – ead Sep 08 '19 at 20:57

1 Answers1

0

I was able to compile this after making several changes.

  1. Remove using namespace std from customer.h. It's bad practice to use "using namespace" here as it could cause name collisions when another file includes customer.h. As a result of removing "using namespace std" you'll need to qualify all of your strings as std::string instead of string.

  2. You declared a default constructor for Account but there is no implementation provided. When a customer is created it tries to call the default constructor of account. If you are on a newer version of c++ you can change this line in your header file.

    Account();
    

    to

    Account() = default;
    

If you're on an older version add this to Account.cpp

Account(){}