q: inheretence/virtual

L

laniik

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
 
C

CrayzeeWulf

laniik said:
class base {
public:
static int x;
base() {cout<<x<<endl;}

virtual void hi();
};

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() { }
};
 
L

laniik

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
 
R

Rolf Magnus

laniik said:
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

Well, if there are multiple definition of base::x, then you must have
defined it multiple times yourself.
 
C

CrayzeeWulf

laniik said:
does anyone know what i can do about the second issue? tnx again

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,
 
C

CrayzeeWulf

laniik said:
does anyone know what i can do about the second issue? tnx again

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,
 
J

Jay Nabonne

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

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
 
P

Peter Koch Larsen

laniik said:
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

Probably you declared x in a header-file. This is wrong. Instead declare it
in a cpp-file.

/Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,664
Latest member
RoseannBow

Latest Threads

Top