Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 23rd, 2005, 04:14 AM
laniik
Guest
 
Posts: n/a
Default 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

  #2  
Old July 23rd, 2005, 04:14 AM
CrayzeeWulf
Guest
 
Posts: n/a
Default 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
  #3  
Old July 23rd, 2005, 04:14 AM
laniik
Guest
 
Posts: n/a
Default 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

  #4  
Old July 23rd, 2005, 04:14 AM
Rolf Magnus
Guest
 
Posts: n/a
Default 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.

  #5  
Old July 23rd, 2005, 04:14 AM
CrayzeeWulf
Guest
 
Posts: n/a
Default 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
  #6  
Old July 23rd, 2005, 04:14 AM
CrayzeeWulf
Guest
 
Posts: n/a
Default 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
  #7  
Old July 23rd, 2005, 04:14 AM
Jay Nabonne
Guest
 
Posts: n/a
Default 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
  #8  
Old July 23rd, 2005, 04:15 AM
Peter Koch Larsen
Guest
 
Posts: n/a
Default 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


  #9  
Old July 23rd, 2005, 04:16 AM
laniik
Guest
 
Posts: n/a
Default Re: q: inheretence/virtual

you're exactly right peter. thanks

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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.
Post your question now . . .
It's fast and it's free

Popular Articles