Class name does not recognize a type

Hi,
I've been working on a program and I've hit an error. I have two 2 classes(in 2 separate h files) and I've included them both inside one another. In my first h file, however, the dealers.h, I've made a pointer of type cars(which is the other class/h file) and when I try to compile, I get the error that cars does not name a type. Additionally, the #ifndef part in the dealers.h is underlined in red and says unterminated conditional directive. I'd appreciate any help, thanks.

#ifndef DEALERS_H
#define DEALERS_H
#include <string>
#include "Cars.h"
using namespace std;

class Dealers {

private:

string dealerName;
int dealerNumber;
int numberOfCars;

public:

Dealers();
Dealers (string _dealerName, int _dealerNumber, Cars *carPtr);
Cars *carArrayPtr = nullptr;

//other code
};
#endif //DEALERS_H
Last edited on
I don't see anything wrong with what you've posted.

It is highly recommended to not say "using namespace std" inside a header file, but that's not the problem here.

And you should post your code between [code] [/code] tags to maintain indentation.
Last edited on
Yeah, I've heard using namespace std isn't the best to do but I'm a beginner and this is how I've been taught and what my instructor prefers in our assignments.

As for the code indentation comment, I will keep that in mind.

I still don't have any idea about why this isn't working either, do you think that trying it as a new project would make a difference?
closed account (SECMoG1T)
can you post the code in Cars.h
closed account (E0p9LyTq)
Post your Cars.h header file and the exact errors you are getting please.

And PLEASE use code tags, it will make reading and commenting on your source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
The short answer is that you should add class Cars; in dealers.h before the definition of class Dealers, and you should add class Dealers; in Cars.h before the definition of class Cars. This tells the compiler that these classes exist and will be declared later.

Now the long answer. I suspect that Cars.h looks something like this:
1
2
3
4
5
6
7
8
9
#ifndef CARS_H
#define CARS_H
...
#include "Dealers.h"
...
class Cars {
...
};
#endif 


Now consider what happens if your main program has #include "Cars.h" :
- The pre-processor includes Cars.h. CARS_H isn't defined at line 1 so it defines it at line 2.
- At line 4, Cars.h includes Dealers.h.
- Dealers.h includes Cars.h. But this time CARS_H is defined, so the pre-processor skips over the bulk of Cars.h.
- The preprocessor continues with Dealer.h. It defines class Dealers and then sees the reference to the undefined class Cars.
- When the preprocessor finishes with Dealers.h, it returns to line 5 of Cars.h.
- Now it sees the definition of class Cars.
I've heard using namespace std isn't the best to do but I'm a beginner and this is how I've been taught and what my instructor prefers in our assignments

I didn't say to not ever say "using namespace std".
I said to never EVER say it in a header file (at global scope).
If your instructor allows that, then he's an idiot.
Last edited on
So I found out the issue, apparently while I had included "cars.h" in my dealers.h file, I had also included "Dealers.h" in my "cars.h" file so it was creating a circle effect.

Thanks for all your help
Topic archived. No new replies allowed.