Thread: virtual functions not working?

  1. #1
    Registered User
    Join Date
    Feb 2012

    virtual functions not working?

    I am learning object oriented programming with C++ and I am having trouble understanding virtual functions and polymorphism.

    I have this simple program:

    PHP Code:
    #ifndef TEST_H_#define TEST_H_typedef struct data {    int test;} data_t;class base {public:    base ();    void test ();    virtual ~base();    virtual int dispatch (int test);};#endif /* TEST_H_ */ 
    PHP Code:
    #include <iostream>#include "test.h"base::base (){}base::~base(){}void base::test (){    dispatch (5);}int main (void){    //base *b = new base ();    //b->test();    return 0;} 
    PHP Code:
    #ifndef TEST2_H_#define TEST2_H_#include "test.h"class derived : public base {public:    derived () {}    virtual ~derived ();    virtual int dispatch (int test);};#endif /* TEST2_H_ */ 
    PHP Code:
    #include "test2.h"#include <iostream>#include <stdio.h>using namespace std;int derived::dispatch (int test){    printf ("test");    return 1;} 
    I'm building with MinGW on windows 7.

    I get this error message:
    test.o:test.cpp.rdata$_ZTV4base[vtable for base]+0x10): undefined reference to `base::dispatch(int)'
    collect2: ld returned 1 exit status
    I am not sure what i'm doing wrong.

    All i want to do is to have two classes (one base, one derived) and have virtual functions in both. I want to be able to call the virtual function in the base class and run the code in the derived.

    Any help is appreciated

  2. #2
    Join Date
    Dec 2007
    Look at your post. All your code is is on *one line*. Please fix it; no one is going to try to sort this out in the state it's in.

  3. #3
    Join Date
    Oct 2007
    Inside my computer
    Additionally, put it in code tags, not PHP tags.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    I am not sure what i'm doing wrong.
    I find the error message the compiler gives you to be very strait forward and infinitely "googalable".


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Virtual functions
    By SterlingM in forum C++ Programming
    Replies: 12
    Last Post: 11-03-2009, 10:51 AM
  2. Replies: 8
    Last Post: 01-18-2008, 03:06 AM
  3. Replies: 4
    Last Post: 01-13-2008, 01:14 AM
  4. Virtual functions but non-virtual destructor
    By cunnus88 in forum C++ Programming
    Replies: 4
    Last Post: 03-31-2007, 11:08 AM
  5. Virtual functions not working
    By VanJay011379 in forum C++ Programming
    Replies: 2
    Last Post: 07-25-2002, 12:12 PM
Website Security Test