Polymorphism problem with virtual functions ...

J

Jonas Huckestein

Hello,

I have the following problem: I have one BaseClass, which defines the
virtual functions getValue(int index) and virtually overloads the operators
*, /, -, +. Now I want two classes SubClass1 and SubClass2 which extend the
BaseClass and implement the virtual functions, so that I can go like this:

BaseClass *oskar = new SubClass1;
BaseClass *newton = new SubClass2;
BaseClass *pete = oskar*newton+newton/oskar;

Is there a way I can accomplish this? Somehow I can't even get the easiest
situation straight, in which I have only two classes and 1 virtual
function:

class BaseClass {
public:
virtual int getValue( int index );
};

class SubClass1 : public BaseClass {
int getValue( int index ) { return 1; };
};

....

int main() {
BaseClass* oskar = new SubClass1;
cout << oskar->getValue(4); // supposed to be 1
delete oskar;
}

Now the compiler tells me: "undefined reference to `vtable for BaseClass'"
and "undefined reference to `typeinfo for BaseClass'"

I use g++-4.1. as a compiler, although I suppose the problem is due to my
lack of understandding C++ ...

Thanks in advance and greetings from Germany,
Jonas
 
R

ruka_at_

Hello,

I have the following problem: I have one BaseClass, which defines the
virtual functions getValue(int index) and virtually overloads the operators
*, /, -, +. Now I want two classes SubClass1 and SubClass2 which extend the
BaseClass and implement the virtual functions, so that I can go like this:

BaseClass *oskar = new SubClass1;
BaseClass *newton = new SubClass2;
BaseClass *pete = oskar*newton+newton/oskar;

Is there a way I can accomplish this? Somehow I can't even get the easiest
situation straight, in which I have only two classes and 1 virtual
function:

class BaseClass {
public:
virtual int getValue( int index );

};

either make the method pure virtual, like this:
virtual int getValue( int index )=0;
or provide a body:
virtual int getValue( int index ){return 0;}
lg Rudi
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Hello,

I have the following problem: I have one BaseClass, which defines the
virtual functions getValue(int index) and virtually overloads the operators
*, /, -, +. Now I want two classes SubClass1 and SubClass2 which extend the
BaseClass and implement the virtual functions, so that I can go like this:

BaseClass *oskar = new SubClass1;
BaseClass *newton = new SubClass2;
BaseClass *pete = oskar*newton+newton/oskar;

Note that this will not do what you want it to do. This will perform
arithmetic operations with the pointers and not with the objects, you
have to dereference the pointers first:

BaseClass *pete = *oskar * (*newton) + *newton / *oskar;

And unless SubClass + SubClass returns a pointer you should use

BaseClass *pete = new SubClass(*oskar * (*newton) + *newton / *oskar);

It would probably be easier to use references/local variables instead.
 
R

Rolf Magnus

Jonas said:
Hello,

I have the following problem: I have one BaseClass, which defines the
virtual functions getValue(int index) and virtually overloads the
operators *, /, -, +.

Making those operators virtual is most often not useful.
Now I want two classes SubClass1 and SubClass2 which extend the BaseClass
and implement the virtual functions, so that I can go like this:

BaseClass *oskar = new SubClass1;
BaseClass *newton = new SubClass2;
BaseClass *pete = oskar*newton+newton/oskar;

Is there a way I can accomplish this? Somehow I can't even get the easiest
situation straight, in which I have only two classes and 1 virtual
function:

class BaseClass {
public:
virtual int getValue( int index );
};

That class is missing a virtual destructor, so the result of your delete in
main is undefined.
class SubClass1 : public BaseClass {
int getValue( int index ) { return 1; };
};

...

int main() {
BaseClass* oskar = new SubClass1;
cout << oskar->getValue(4); // supposed to be 1
delete oskar;
}

Now the compiler tells me: "undefined reference to `vtable for BaseClass'"
and "undefined reference to `typeinfo for BaseClass'"

I use g++-4.1. as a compiler, although I suppose the problem is due to my
lack of understandding C++ ...

Except for the non-virtual destructor, your code is ok, so it must be
something else.
Thanks in advance and greetings from Germany,

Greetings back from Germany ;-)
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top