?
Solved

C++: I have an issue with redefinition of classes...

Posted on 2005-05-01
4
Medium Priority
?
268 Views
Last Modified: 2010-04-17
Hello experts,

This problem should be easily solvable... Here is the situation:

1) I have 4 classes ( class A, B, C & D ).

2) Functions from class A are needed in class B & C, so I have #inlcluded "A" in both classes.

3) Class D needs both class B & C.  And this means that class A is #included twice, so the compiler is complaining that I am redefining them.

HOW DO I GET AROUND THIS?

Thank you experts,

-TH
0
Comment
Question by:travishaberman
  • 3
4 Comments
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 2000 total points
ID: 13905257
Hi TH,

You need to use Macro's to prevent duplications:


// Class A
class A
{
   // Functions, etc..
}


// Class B

#ifndef CLASS_A
  #include "A"                           // Incluce the A class here
  #define CLASS_A
#endif
class B
{
   // Functions, etc..
}


// Class C

#ifndef CLASS_A
  #include "A"                           // Incluce the A class here
  #define CLASS_A
#endif
class C
{
   // Functions, etc..
}


// Class D

#include "B"             // Import B & C
#include "C"            //
class D
{
   // Functions, etc..
}



Hope that helps.
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13905283
Sorry, as you probably know, you need to end the class {} declarations with a semi-colon, like so:

// Class A
class A
{
   // Functions, etc..
}

Should be:

// Class A
class A
{
   // Functions, etc..
};

// etc.

:-)

(I've been doing so much Java recently, I've got into the habbit of not using the semi-colon for classes)  ;-)
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13905293
Usual way is to define "header guards" in each header (.h) file:

Class A header file:

#ifndef  _HEADER_CLASS_A
#define _HEADER_CLASS_A

// all your file contents here

#endif

Do the same for all header files in your project, it is a good practice and you will never have a duplicated header problem.

0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13905614
Cheers. :-)

Take note of Jaime's comment though.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Screencast - Getting to Know the Pipeline

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question