Thread: errors with virtual functions

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    39

    errors with virtual functions

    I have declared these classes, as below, but I get repeatedly the following compile errors. Could you explain to me what is wrong?

    Code:
    class A{
          public:
                virtual void f1();
    };
    
    class A1: public A{
          public:
            void f1();
    };
    
    class A2: public A{
          public:
            void f1();
    };
    Code:
    variable or field `f1' declared void
    `f1' declared as a `virtual' field
     expected `;' before '(' token

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    define

    what compiler are you using? i would have expected a different error so perhaps something else is going on that you have not posted, but in any case if you declare a virtual function then you also have to provide an implementation, so just place a pair of curly braces after the declaration in your class definition
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    39
    Quote Originally Posted by rogster001 View Post
    what compiler are you using? i would have expected a different error so perhaps something else is going on that you have not posted, but in any case if you declare a virtual function then you also have to provide an implementation, so just place a pair of curly braces after the declaration in your class definition
    I am using the Dev C++. I have also provided the virtual functions implementations along with the child class definitions. I just removed them, when posting the code.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The only way to get that message is to have
    Code:
    virtual void f1;
    instead of
    Code:
    virtual void f1();
    The missing ; probably just brings the point home that you need to check your parentheses and braces and make sure they line up where they're supposed to.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I'm not sure what the problem is but MinGW GCC 4.5 can compile your code as is. To get the same amazing result, you could just upgrade your compiler (which means throwing Dev-C++ away probably) and try this orthis .

  6. #6
    Registered User
    Join Date
    Dec 2010
    Location
    China
    Posts
    7
    I am using the Dev C++ (vc 2005 TooL), can compile your code .

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    39
    Well, I had to test another compiler simply (Microsoft visual C++ 2010).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. virtual functions
    By lord in forum C++ Programming
    Replies: 9
    Last Post: 10-18-2008, 02:55 PM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. i need a good example of virtual functions
    By Anddos in forum C++ Programming
    Replies: 10
    Last Post: 02-15-2006, 11:48 AM
  4. Replies: 2
    Last Post: 10-02-2005, 05:04 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM