Computing.Net > Forums > Programming > C++ class redefinition problems

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ class redefinition problems

Reply to Message Icon

Name: Pixy
Date: May 9, 2002 at 11:10:10 Pacific
Comment:

I am experiencing the following problems when trying to compile my code using Microsoft Visual C++:

class type redefinition errors

Basically, I am to have one main "parent class" definition file, with the parent class implementations file, along with three subclasses, their implementation and definitions in a separate file.

I "#include "parentimplementation.h" in them all...

I get the error then about class type redefinition errors when I compile. I have been told I may do something like

"#ifndef parentimp
#define parentimp
#endif"

to get rid of these. I did not get specific info about where to stick this code, but I imagine it is within all the files. I have tried this, still get 40+ errors...

Any suggestions would be much appreciated!

Pixy



Sponsored Link
Ads by Google

Response Number 1
Name: Jeff
Date: May 9, 2002 at 11:28:53 Pacific
Reply:

The preprocessor code it was suggested you add, is also known as an inclusion guard. It helps avoid re-referencing the same code. However, if you add it as shown above, it won't work. It's done more like this:

//very beginning of header file
#ifndef MY_HEADER_FILES_GUARD_IDENTIFIER
#define MY_HEADER_FILES_GUARD_IDENTIFIER

//put all your header's code in here
//...

#endif //this is last line of code


That way, if by accident you get multiple inclusions, the inclusion guard will stop any "redefinition" of the code within it. Of course, each header's #defined "guard identifier" must be unique.


0

Response Number 2
Name: Jeff
Date: May 9, 2002 at 11:32:48 Pacific
Reply:

P.S. - For reasons I cannot comprehend, VC++ does not automatically add inclusion guards when you create a new header file. Yet, every one of their own headers have them, as do most peoples'. I wrote a macro to automatically do this boring chore for each new header ;)


0

Response Number 3
Name: Pixy
Date: May 9, 2002 at 11:46:20 Pacific
Reply:

HI!

Thanks a lot for the help! I have a quick question again... sorry for being such a twit!

I will put a sample of my code below. I am just unsure of what you mean by
"MY_HEADER_FILES_GUARD_IDENTIFIER"

Here, students is the parent class, and I have included below a sample subclass header file, FineArtsStudent.h - I was wondering if you could show me where I should put the code? I know you did it above, but I am still a little confused:)

Pixy


here is the subclass.. There might be some weirdness here because of the way it posts sometimes.. sorry for that!

-------------------------

#include "Studentimp.h"

class FineArts: public Students
{
private:
float theoryGrade;
float paintingGrade;
float sculptureGrade;


public:
float FAFinalGrade();
FineArts(): Students(), theoryGrade(0), paintingGrade(0), sculptureGrade(0){};
FineArts(int s, char *n, float t, float p, float sc): Students(s, n), theoryGrade(t), paintingGrade(p), sculptureGrade(sc){};
friend ostream& operator << (ostream& out, FineArts& FAStudent);

};

float FineArts::FAFinalGrade()
{
return (((3 * theoryGrade) + (2 * paintingGrade) + (2 * sculptureGrade)) / 7);
}

ostream& operator << (ostream& out, FineArts& FAStudent)
{
FAStudent.finalGrade = FAStudent.FAFinalGrade();

out << FAStudent.name << " " << FAStudent.studentNumber << ": " << FAStudent.finalGrade << endl;

return out;

}


0

Response Number 4
Name: Jeff
Date: May 9, 2002 at 12:19:57 Pacific
Reply:

Yeah, at brief glance, that all looks fine (posting here is not exactly code-friendly!). You just need to put all that within an inclusion guard, and all your other headers within their own guards. That is, put the header code after the "#define MY_HEADER_FILES_GUARD_IDENTIFIER" line, but before the last "#endif" that completes the guard.

The #defined macro "MY_HEADER_FILES_GUARD_IDENTIFIER" was just an attempt to explain what that identifier was for. In actual use, you can put any non-spaced text you wish, but it's very common to use some variation on your header's filename.

For example, if my header's name is "ClassHelpers.h", I typically make my inclusion guard identifier "_CLASSHELPERS_H_" or something. As long as it's unique to the header file it's in. Micro$oft is fond of even simpler naming, such as "_WINDOWS_" as the guard identifier for the classic "Windows.h".


0

Response Number 5
Name: Pixy
Date: May 9, 2002 at 12:50:04 Pacific
Reply:

It works! It works!

Thank-you very very much!!!

Pix


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ class redefinition problems

Question Regarding VB.net and C# www.computing.net/answers/programming/question-regarding-vbnet-and-c/3580.html

c++ class constructor problem www.computing.net/answers/programming/c-class-constructor-problem/1216.html

C++ Problem www.computing.net/answers/programming/c-problem/13694.html