Andreas BH
Andreas BH

Reputation: 9

C++ error code C2065: '<class name>' undeclared identifier, even though it should be declared in another .h-file

I have a multifile program, and I can't figure out why my program says that "Customers" (in the registerNewUser() function) is an undeclared identifier.

proc.h

#ifndef PROC_H
#define PROC_H
#include <iostream>
#include "const.h"
#include "customers.h"
#include <fstream>
using namespace std;

void registerNewUser(Customers cBase); // Add new user.

#endif // !PROC_H

I have included the header file (customers.h) with the Customers class also.

customers.h

#ifndef CUSTOMERS_H
#define CUSTOMERS_H
#include <iostream>
#include "const.h"
#include "proc.h"
#include "customer.h"
using namespace std;

class Customers {
    private:
        char* current;
        List* customerList;     // List for customers.
    public:                        
        Customers();            // Constructor.
        ~Customers();           // Destructor.
        void handler();         // Customers handler/menu.
        void addNew(char username[]);
    };

#endif // !CUSTOMERS_H

Can anyone see what's wrong?

Upvotes: 0

Views: 5618

Answers (3)

Loesgar
Loesgar

Reputation: 44

Basically including "customers.h" in "customers.h" wouldn't be a problem here, since you have a guard (plus point for that). Nevertheless it is not very nice.

As NathanOliver said it COULD be a problem with the order of the includes but it doesn't have to. If you include proc.h first everything is fine. If you include customers first, the compiler includes proc.h before he sees the customer class. proc then wont include customers.h (since its guard prevents it). Then he will find your function not knowing what "Customer" means. So depending on the include order of your header files it will or will not work.

If you want a hint: I normally first only include the necessary files for a forward declaration, then do a forward declaration. Then I include the files necessary files for the definition of the class (These will already know that the class exists). The complete class declaration (with member function declaration) follows. If you do it like this you can avoid many mistakes. In your case:

#ifndef CUSTOMERS_H
#define CUSTOMERS_H

class Customers;

#include "proc.h"
#include "ListTool2B.H"
using namespace std;

class Customers 
{
    private:
        char* current;
        List* customerList;     // List for customers.
    public:                        
        Customers();            // Constructor.
        ~Customers();           // Destructor.
        void handler();         // Customers handler/menu.
        void addNew(char username[]);
};

#endif

Upvotes: 1

NathanOliver
NathanOliver

Reputation: 180990

You have a circular include. customers.h includes proc.h so basiacally

void registerNewUser(Customers cBase);

Will get added to customers.h before the compiler has seen what a Customer is. It looks like you should just be able to remove the #include "proc.h" in customers.h and it should compile.

As stated in the comments above you should never use using namespace std; in a header file as anything that includes it now has the entire std namespace exposed. You should also get in the habit of only using it in the most narrow scope you can or drop it completely. For further reading on the use of using namespace std; see Why is “using namespace std” in C++ considered bad practice?

Upvotes: 5

Marco
Marco

Reputation: 2020

This is probably a duplicate: you have proc.h including customers.h and customers.h including proc.h this will cause a circular reference, and looks like proc.h included in customers is not necessary, so you could try simply to delete this line:

#include "proc.h"

Upvotes: 0

Related Questions