0

I'm trying to use a class within another class but the class(CustomerList) isn't being highlighted in the EmployeeRecord.h but it is in the EmployeeRecord.cpp. This is my EmployeeRecord.cpp

#include "EmployeeRecord.h"
#include <string>
#include <iostream>
#include "CustomerList.h"

using namespace std;

EmployeeRecord::EmployeeRecord()
{
    m_iEmployeeID = 0;
    strcpy(m_sFirstName, "");
    strcpy(m_sLastName,"");                        
    m_iDeptID = 0;
    m_dSalary = 0.0;
    *m_pCustomerList = new CustomerList;
}

This is my EmployeeRecord.h

#pragma once
#include "CustomerList.h"

class EmployeeRecord
{
private:
    int m_iEmployeeID;
    char m_sFirstName[32];
    char m_sLastName[32];
    int m_iDeptID;
    double m_dSalary;
    CustomerList *m_pCustomerList;

In the .cpp the pointer says it's unidentified but I don't know if that's because CustomerList isn't being recognized in the .h or if it's another issue.

  • 1
    In your constructor you're trying to dereference a ptr (`m_pCustomerList *`), and assign the result of a `new` operation to the `CustomerList` object. You likely want to do `m_pCustomerList = new CustomerList;` – JohnFilleau Feb 28 '20 at 22:19
  • 2
    What is the **exact** error message? And which line causes it? Is the error when you compile or is it in your editor? – Code-Apprentice Feb 28 '20 at 22:20
  • Since you're `#include ` already, I also suggest having `m_sFirst/LastName` be `std::string` objects, and to move the assignments in your constructor an an initializer list. – JohnFilleau Feb 28 '20 at 22:22
  • Do you get [`undefined reference to EmployeeRecord::EmployeeRecord()`](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) error? – Yksisarvinen Feb 28 '20 at 22:26

1 Answers1

0

You probably want to use

m_pCustomerList = new CustomerList;

in your .cpp since you want to initialize a pointer (Note the missing * before the member name).

You are trying to give a "value" to the value pointed by m_pCustomerList (which, at this moment, doesn't point to anything since it is uninitialized).

J Faucher
  • 988
  • 6
  • 14
  • 1
    Thanks, I learned something ! The point with the `*` preceding in the constructor still remains, though. – J Faucher Feb 28 '20 at 22:36