Hi,

Suppose i have an animal base class and cat derived class..I create animal.cpp, animal.h, cat.cpp, and cat.h files...Assuming i have included Animal.h in my Cat files, what files do i have to include in the main.cpp file? is it just:

1) #include "Animal.h"

or do i also include:

2) #include "Cat.h"

The reason i ask is because when i try to create a Cat object in main.. with option 1), i get an error saying:

6 C:\Dev-Cpp\main1.cpp `Cat' undeclared (first use this function)

And with option 2) i get an error saying:

6 C:\Dev-Cpp\Animal.h redefinition of `class Animal'


I assume option 1) is the way to go but why am i getting this error??

Help appreciated

Recommended Answers

All 3 Replies

Because you did something wrong. That's why you're getting that error.

And since we can't see your code, that about as much as we can guess at. Your description tells us nothing useful.

Hi,

Suppose i have an animal base class and cat derived class..I create animal.cpp, animal.h, cat.cpp, and cat.h files...Assuming i have included Animal.h in my Cat files, what files do i have to include in the main.cpp file? is it just:

1) #include "Animal.h"

or do i also include:

2) #include "Cat.h"

The reason i ask is because when i try to create a Cat object in main.. with option 1), i get an error saying:

6 C:\Dev-Cpp\main1.cpp `Cat' undeclared (first use this function)

And with option 2) i get an error saying:

6 C:\Dev-Cpp\Animal.h redefinition of `class Animal'


I assume option 1) is the way to go but why am i getting this error??

Help appreciated

Option 2 is the way to go. The rule is that you need to include all the code you need, so since nothing else #includes Cat.h, you need to #include it manually, and your Cat.h will include its parent Animal.h

The reason option 2 isn't working is because it's being included multiple times somehow, which is confusing your compiler. You just need to put these lines into all your .h files..
At the beginning:
#ifndef CAT_H //look up #ifndef online for more information
#define CAT_H


at the end:
#endif
-Greywolf

commented: Informative, and useful..Pleasant poster +1

Greywolf333

Thanks..That works fine for me now :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.