By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 833 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

q: inheretence/virtual

P: n/a
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

Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
laniik wrote:
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() { }
};

--
CrayzeeWulf
Jul 23 '05 #2

P: n/a
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

Jul 23 '05 #3

P: n/a
laniik wrote:
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.

Jul 23 '05 #4

P: n/a
laniik wrote:
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,
--
CrayzeeWulf
Jul 23 '05 #5

P: n/a
laniik wrote:
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,
--
CrayzeeWulf
Jul 23 '05 #6

P: n/a
On Tue, 12 Apr 2005 16:07:58 -0700, laniik wrote:
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
Jul 23 '05 #7

P: n/a

"laniik" <la****@yahoo.com> skrev i en meddelelse
news:11**********************@f14g2000cwb.googlegr oups.com...
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
Jul 23 '05 #8

P: n/a
you're exactly right peter. thanks

Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.