
July 23rd, 2005, 04:14 AM
| | | q: inheretence/virtual
Hi. I have a base class
class base {
public:
static int x;
base() {cout<<x<<endl;}
virtual void hi();
};
int base::x=0;
//and a class that inherets it
class subbase : public base{
public:
void hi() {cout<<"hi"<<end;}
};
when i compile it on g++ i get several problems
1) undefined symbol
vtable for base
typeinfo for base
i think this has to do with the virtual functions... am i using them
incorrectly?
2) base::x is multiply defined
how can i get it so that the sub-classes dont try to redefine x?
Thanks!
Oliver | 
July 23rd, 2005, 04:14 AM
| | | Re: q: inheretence/virtual
laniik wrote:
[color=blue]
> class base {
> public:
> static int x;
> base() {cout<<x<<endl;}
>
> virtual void hi();
> };[/color]
You have declared "hi()" in class base but never defined it. You can do one
of the following, for example:
class base {
public:
static int x;
base() {cout<<x<<endl;}
virtual void hi() = 0 ; // Pure Virtual
// Or
// virtual void hi() { }
};
--
CrayzeeWulf | 
July 23rd, 2005, 04:14 AM
| | | Re: q: inheretence/virtual
ah great, i did not know that a definition was needed for a virtual
function.
that solves problem (1)
does anyone know what i can do about the second issue? tnx again | 
July 23rd, 2005, 04:14 AM
| | | Re: q: inheretence/virtual
laniik wrote:
[color=blue]
> ah great, i did not know that a definition was needed for a virtual
> function.
>
> that solves problem (1)
>
> does anyone know what i can do about the second issue? tnx again[/color]
Well, if there are multiple definition of base::x, then you must have
defined it multiple times yourself. | 
July 23rd, 2005, 04:14 AM
| | | Re: q: inheretence/virtual
laniik wrote:
[color=blue]
> does anyone know what i can do about the second issue? tnx again[/color]
Can you provide your complete program verbatim (you had a syntax error in
the original post) ? For example, the following compiles
and works using g++ (v3.3.5):
#include <iostream>
using namespace std ;
class base {
public:
static int x;
base() {cout<<x<<endl;}
virtual void hi() = 0 ;
};
int base::x=0;
class subbase : public base
{
public:
void hi() {cout<<"hi"<<endl;}
};
int
main()
{
subbase foo ;
return 0 ;
}
Thanks,
--
CrayzeeWulf | 
July 23rd, 2005, 04:14 AM
| | | Re: q: inheretence/virtual
laniik wrote:
[color=blue]
> does anyone know what i can do about the second issue? tnx again[/color]
Can you provide your complete program verbatim (you had an error in
the original post) ? For example, the following compiles
and works using g++ (v3.3.5):
#include <iostream>
using namespace std ;
class base {
public:
static int x;
base() {cout<<x<<endl;}
virtual void hi() = 0 ;
};
int base::x=0;
class subbase : public base
{
public:
void hi() {cout<<"hi"<<endl;}
};
int
main()
{
subbase foo ;
return 0 ;
}
Thanks,
--
CrayzeeWulf | 
July 23rd, 2005, 04:14 AM
| | | Re: q: inheretence/virtual
On Tue, 12 Apr 2005 16:07:58 -0700, laniik wrote:
[color=blue]
> ah great, i did not know that a definition was needed for a virtual
> function.
>
> that solves problem (1)
>
> does anyone know what i can do about the second issue? tnx again[/color]
Be sure you don't include:
int base::x=0;
in a header file. It needs to be defined in exactly one cpp file.
- Jay | 
July 23rd, 2005, 04:15 AM
| | | Re: q: inheretence/virtual
"laniik" <laniik@yahoo.com> skrev i en meddelelse
news:1113344396.484125.315630@f14g2000cwb.googlegr oups.com...[color=blue]
> ah great, i did not know that a definition was needed for a virtual
> function.
>
> that solves problem (1)
>
> does anyone know what i can do about the second issue? tnx again
>[/color]
Probably you declared x in a header-file. This is wrong. Instead declare it
in a cpp-file.
/Peter | 
July 23rd, 2005, 04:16 AM
| | | Re: q: inheretence/virtual
you're exactly right peter. thanks |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|