redefinitions?

when u have a base class that's used by two other class, C++ gives an error in redefinition. how can i solve this? i think i'm suppose to yse #ifndef and #define but i'm not sure how to use them. thanks
Liquid111199Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
basantConnect With a Mentor Commented:
What leflon has said is perfectly OK. I will try to explain it in my own words.

See suppose you have baseclass B
and Derive class D1 & D2.
class B {};
class D1 : public B { };
class D2 : public B { };

To avoid the problem of redefinition
there r two possibility
(1) You define all three class in one header file that is say b.h and the order I mentioned above.
(2) If u want to define in separate File, use it like this :
File b.h
#ifndef B_DEFINED
#define B_DEFINED

class B
{
};
#endif

File D1.h
#ifndef D1_DEFINED
#define D1_DEFINED
#include "b.h"

class D1 : public B
{
};
#endif


File D2.h
#ifndef D1_DEFINED
#define D1_DEFINED
#include "b.h"

class D2 : public B
{
};
#endif
0
 
leflonCommented:
if you use a header file witjh your class definition try something like:

xyz.h

#ifndef __MYCLASSABC_H__
define __MYCLASSABC_H__

class ABC
{
....
};

#endif

now you should be able to include this header into as many files as you need without getting an error.
0
 
ResonanceCommented:
If you're referring to a base class with functions that are overridden by other classes derived from that base class, you need to make sure that each of those functions is declared virtual,

i.e.:

class mybase
{
private:
public:
  virtual int funct1(int a);
  virtual float funct2(float a);
}

class mysub1 : public mybase
{
private:
public:
  int funct1(int a, int b=4);
  float funct2(float a, float b=5);
}

class mysub2 : public mybase
{
private:
public:
  int funct1(int a);
  float funct2(float a);
}


Is that what you meant?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Liquid111199Author Commented:
Great answers, all of them but can u pls tell me how does #ifndef work? what do u ahve to put after that keyword? it seems to always have a underscore somewhere in that word, how does it work? thanks
0
 
ResonanceCommented:
#ifndef is short for "If Not Defined".  The word after it is merely a token, or name.  All it's doing is saying "If this word has not yet been defined with a #define, then do all of the next commands until the #endif.  If it has been, skip all of the commands between here and the #endif."
0
 
basantCommented:
You might be knowing that this is
done at the time of pre-processing
i.e. before compilation.
0
 
nietodCommented:
>> If you're referring to a base class with functions
>> that are overridden by other classes derived from
>> that base class, you need to make sure that each
>> of those functions is declared virtual,
That is not true.

It also has nothing to do with this problem.
0
 
Liquid111199Author Commented:
thanks alot everyone, i get it now. sorry for the late reply.
0
All Courses

From novice to tech pro — start learning today.