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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
basantCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.