-1

Not sure what I'm doing wrong, was hoping someone can point me in the right direction. I created a customer class but I cannot initialize it.

customer.h

#ifndef CUSTOMER_H
#define CUSTOMER_H

#include "defs.h"
#include <string>
using namespace std;

class Customer
{
  public:
    Customer(string fName, string lName);
    string       getFirstName();
    string     getLastName();
    int        getCustID();
    int        getNumAccounts();

  protected:
    string     firstName;
    string     lastName;
    int        custID;
    int        numAccounts;

};

#endif

customer.cc

#include "defs.h"
#include "Customer.h"


int Customer::nextCustID = 9001;

void Customer(){
    //nothing;
}

void Customer::Customer(int test, int tes2)
{
    custID = 100;
    firstName = "George";
    lastName = "sadfsad";
    numAccounts = 0;
}


void Customer::Customer(string fName, string lName)
{
  custID  = nextCustID++;
  string firstName = fName;
  string lastName = lName;
  numAccounts  = 0;
}

int      Customer::getCustID()  { return custID;  }
string     Customer::getFirstName()     { return firstName;     }
string     Customer::getLastName()     { return lastName;     }
int      Customer::getNumAccounts()  { return custID;  }

I am trying to initialize the customer with

Customer test("Billy", "Bob");

But then I get the error when I try to initialize

BankControl.cc:(.text+0xaf): undefined reference to `Customer::Customer(std::string, std::string)'
collect2: error: ld returned 1 exit status

I can't figure out what I'm doing wrong, if anyone has some input, that would be great. Thanks a lot in advance.

Bankcontrol.cc

#include "BankControl.h"
#include "Account.h"
#include "Customer.h"

#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include "View.h"

BankControl::BankControl()
{
  Customer test("Joe", "Billy");
}
user1730056
  • 619
  • 3
  • 11
  • 19

4 Answers4

1
void Customer::Customer(string fName, string lName)
{
  custID  = nextCustID++;
  string firstName = fName;
  string lastName = lName;
  numAccounts  = 0;
}

The constructor is not a void function. So, remove the "void". Also, you probably want to initialize variables rather than assigning them.

Customer::Customer(string fName, string lName) : firstName(fName), ....

Sorry I did not do more, but without defs.h, it's difficult. Hope this helps.

John Griffin
  • 377
  • 2
  • 8
  • There wasn't anything used from def.h anymore so I removed it completely. – user1730056 Sep 29 '16 at 21:01
  • Note that with c++11 you can actually initialize variables with default values in the header file. That would allow setting `int nextCustID=9001;` in the header rather than the `.cc` file. – Jvinniec Sep 29 '16 at 21:01
  • He should also add `Customer() ;` and `Customer(int test, int tes2);` to his header file as well as change the default constructor in his `.cc` file from `void Customer()` to `Customer::Customer()`. – Jvinniec Sep 29 '16 at 21:04
  • @Jvinniec: where is `Customer::nextCustID` declared? `The `::` scope operator says its part of the `Customer` class or the `Customer` namespace. So, where is it? – Thomas Matthews Sep 29 '16 at 21:05
  • @Thomas Matthews - You're absolutely correct. Your answer points this out. He basically needs to completely remove the `int Customer::nextCustID` from his `.cc` file and at least add `int nextCustID;` to either the public or (more likely) the protected sections of `customer.cc`. My suggestion only prevents him from having to include a default initialization for that variable in _every single_ constructor (but only in c++11). – Jvinniec Sep 29 '16 at 21:08
0

Since you asked what you are doing wrong, here are some issues that I have identified.

"defs.h" not needed.
The class Customer looks self contained except for the std::string, which is defined by <string>.

using namespace std in header
Not a good idea. This means that the std namespace will be included (opened up) for every source file that includes this header.

Pass constant variables by constant reference
Your methods are not modifying their string parameters so pass them by constant reference:

Customer(const std::string& fName, const std::string& lName)  

The reference will allow the compiler to generate code that accesses the variables directly rather than passing copies. Sometimes making copies of large variables takes time and extra space.

Missing identifier: nextCustId
The line:

int Customer::nextCustID = 9001;

says that nextCustID is a member of class Customer, but the data member doesn't exist in the class declaration that you originally posted.

Constructors don't have return types Don't specify the return types for constructors, they are special functions that don't require a specified return type.

Constructor doesn't use parameters
The constructor

Customer(int test, tes2)

doesn't use its parameters.

Thomas Matthews
  • 54,980
  • 14
  • 94
  • 148
  • I'd also point out that `Customer.h` is missing declarations of `Customer()` and `Customer(int test, int test2)` – Jvinniec Sep 29 '16 at 21:17
0

You are compiling and linking only one file Bankcontrol.cc. So linker cannot find ctor for class Customer which is defined in file customer.cc. You need to configure your IDE or build system to compile and link all source files for your project and then, after you fix compilation errors for all files it should work.

Details can be found here:

What is an undefined reference/unresolved external symbol error and how do I fix it?

How to link multiple implementation files in C

Thow last topic about C - C++ compilation and linking work the same way.

Community
  • 1
  • 1
Slava
  • 42,063
  • 1
  • 43
  • 86
  • Even if you include the appropriate linking of the customer.h and customer.cc files, the asker is still going to have the same problem. I think the other answers are closer to the mark on the actual issue. – Jvinniec Sep 29 '16 at 21:14
  • @Jvinniec according to OP's own answer mine was the closest one despite your doubts. – Slava Sep 29 '16 at 21:19
  • Because `void Customer::Customer(string fName, string lName)` is not a valid way of defining a constructor. At least GCC seems to complain about it. – Jvinniec Sep 29 '16 at 21:21
  • As I said "after you fix compilation errors". That compilation errors would come later, but it was not the reason for failure OP asked about. – Slava Sep 29 '16 at 21:23
-1

Forgot to put Customer.o in my makefile. That solved everything.

user1730056
  • 619
  • 3
  • 11
  • 19
  • 2
    As this answer and question as well has zero value for other people you may want to remove your question. – Slava Sep 29 '16 at 21:20