Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error C2514 class has no constructors. But it does?

Tags:

c++

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;
};
like image 851
Shannon Birch Avatar asked Nov 20 '15 10:11

Shannon Birch


1 Answers

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

like image 65
king_nak Avatar answered Sep 19 '22 23:09

king_nak