Polymorphism problem with virtual functions ...

Discussion in 'C++' started by Jonas Huckestein, Feb 12, 2007.

  1. 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
     
    Jonas Huckestein, Feb 12, 2007
    #1
    1. Advertising

  2. Jonas Huckestein

    Guest

    On 12 Feb., 12:15, Jonas Huckestein <> wrote:
    > 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
     
    , Feb 12, 2007
    #2
    1. Advertising

  3. On Feb 12, 12:15 pm, Jonas Huckestein <> wrote:
    > 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.

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Feb 12, 2007
    #3
  4. Jonas Huckestein

    Rolf Magnus Guest

    Jonas Huckestein wrote:

    > 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 ;-)
     
    Rolf Magnus, Feb 12, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Krivenok Dmitry
    Replies:
    13
    Views:
    1,443
    Axter
    Jun 1, 2006
  2. John Goche
    Replies:
    10
    Views:
    754
    Marcus Kwok
    Dec 8, 2006
  3. newbie
    Replies:
    6
    Views:
    405
  4. desktop
    Replies:
    7
    Views:
    321
    James Kanze
    May 11, 2007
  5. Belebele
    Replies:
    8
    Views:
    2,293
    naoki
    Dec 13, 2007
Loading...

Share This Page