virtual functions not working?

This is a discussion on virtual functions not working? within the C++ Programming forums, part of the General Programming Boards category; Hi I am learning object oriented programming with C++ and I am having trouble understanding virtual functions and polymorphism. I ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    1

    virtual functions not working?

    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

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    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
    Posts
    4,359
    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".

    Soma

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, 11:51 AM
  2. Replies: 8
    Last Post: 01-18-2008, 04:06 AM
  3. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  4. Virtual functions but non-virtual destructor
    By cunnus88 in forum C++ Programming
    Replies: 4
    Last Post: 03-31-2007, 12:08 PM
  5. Virtual functions not working
    By VanJay011379 in forum C++ Programming
    Replies: 2
    Last Post: 07-25-2002, 01:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21