0

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?

Andreas BH
  • 9
  • 2
  • 6
  • Check that you haven't messed up the #include guards in your other headers. Better yet, remove all the irrelevant headers completely. Also, ProTip™: never put `using namespace std;` in a header - ever. – Paul R Apr 06 '16 at 11:51

3 Answers3

5

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?

Community
  • 1
  • 1
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • Thanks for the answer! I have a function inside of proc.h which I use in the Customers class, though, so I can't really remove it. Is there another solution? Also, customer.h is another file. This one is called customerS.h (with an extra s), and is a sort of container class for customer.h. – Andreas BH Apr 06 '16 at 11:56
  • @AndreasBH Yes. You remove that functionality from `proc.h` and add it to a cpp file(`proc.cpp`?) and then include `customers.h` and `proc.h` into that cpp file. Generally header files should just contain declarations to make the compiler aware of objects and functions and cpp files should have definitions. – NathanOliver Apr 06 '16 at 11:58
  • Yeah, my h files only have declarations, and my cpp files (with the same name) have the definitions. I tried removing the "#include proc.h" from the customers.h file, but I still get the undeclared identifier error. Any clue? – Andreas BH Apr 06 '16 at 12:06
  • @AndreasBH From what you posted it should work. You could try forward declaring `Customers` in `proc.h` but you shouldn't need to. I would suggest you try boiling down the code to an [mcve] as that should help to figure out where in the chain things are broken. right now you have a lot more code than what is needed to see of including a class in one file from another works or not. – NathanOliver Apr 06 '16 at 12:17
1

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
Loesgar
  • 44
  • 3
0

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"
Marco
  • 1,952
  • 1
  • 17
  • 23