Hi
I am learning object oriented programming with C++ and I am having trouble understanding virtual functions and polymorphism.
I have this simple program:
Code:
test.h
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_ */
test.cpp
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;}
test2.h
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_ */
test2.cpp
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
Thanks