Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

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.

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 ;)

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;
}

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".

![]() |
![]() |
![]() |

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