7

I'm getting:

Error 1 error C2514: 'EmployeeListNode' : class has no constructors

in ListOfEmployee.cpp

But in EmployeeListNode.h I have:

class EmployeeListNode
{
    friend class ListOfEmployee;
public:
    EmployeeListNode(string name, double salary); //A constructor no?
};

I don't understand why it won't recognise that as a constructor. Sorry if this is a stupid question, but I couldn't find an answer through searching.

Edit: The section of the ListOfEmployee that's giving the errors:

void ListOfEmployee::insert(string nameIn, double salaryIn){
EmployeeListNode *n1 = new EmployeeListNode(nameIn, salaryIn);
EmployeeListNode* tn;
if (head){
    head = n1;
}else{
    for (tn = head; tn->next; tn = tn->next);
}

} Edit 2: And the ListOfEmployee.h in case it makes a difference:

#pragma once
#include<string>
using namespace std;
class EmployeeListNode;
class ListOfEmployee
{
public:
    ListOfEmployee();
    void insert(string name, double salary);
    void display();
    void deleteMostRecent();
    double getSalary(string name);
    ~ListOfEmployee();
private:
    EmployeeListNode *head;
};
2
  • Looks like you're trying to default-construct an EmployeeListNode somewhere. Maybe you have one as a class member and don't initialize it in the initialization list? Please post where the error comes from. Commented Nov 20, 2015 at 10:32
  • I edited it to add the section of ListOfEmployee.cpp that is giving the error. Commented Nov 20, 2015 at 10:34

1 Answer 1

17

It can be that you only forward-declared ListOfEmployeeNode, without including it's header (and therefore definition) where it is used.

In that case, the compiler knows about the class, but cannot access any members, including constructors.

If you did include the header, check your include guards. If they happen to be same in the two header files, the definition can be discarded by the preprocessor

5
  • Yes that was it I forgot to include it in the cpp. I feel like an idiot now. <br />Thank you so much! Commented Nov 20, 2015 at 10:40
  • 1
    +1 Exactly as demonstrated in the documentation for error C2514
    – Clifford
    Commented Nov 20, 2015 at 10:40
  • @Clifford it doesn't say that there. Commented Nov 20, 2015 at 10:45
  • @ShannonBirch : My comment refers to the answer, not your comment; and it exactly describes the scenario described in the first paragraph. The example shows the class f forward declared but not defined as suggested in this answer. Even without the header, a forward declaration of ListOfEmployeeNode must have been visible in your code otherwise you'd have had an entirely different error.
    – Clifford
    Commented Nov 20, 2015 at 12:39
  • Add a #pragma once / include guard fixed it
    – mouse
    Commented Sep 17, 2018 at 8:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.