Liquid111199
asked on
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
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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
#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."
You might be knowing that this is
done at the time of pre-processing
i.e. before compilation.
done at the time of pre-processing
i.e. before compilation.
>> 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.
>> 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.
ASKER
thanks alot everyone, i get it now. sorry for the late reply.
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.