Pure virtual function error

This is a discussion on Pure virtual function error within the C++ Programming forums, part of the General Programming Boards category; Here, I have a class which is intended to be a superclass so that I can put a bunch of ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    14

    Pure virtual function error

    Here, I have a class which is intended to be a superclass so that I can put a bunch of its subclasses in a set.

    Code:
    class Drawable
    {
    	public:
    	virtual void draw(BITMAP* b) =0;
    	
    };
    Here are the errors I get:

    2 In file included from Player.cpp
    5 variable or field `draw' declared void
    5 `draw' declared as a `virtual' field
    5 expected `;' before '(' token
    [Build Error] [Player.o] Error 1

    I'm compiling it with DevC++.

    I see no problems here...

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    Is BITMAP declared at that point?

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You haven't declared BITMAP, and that confuses the compiler (it seems - I can't explain why it's not producing a more appropriate error message), but this compiles fine:
    Code:
    struct BITMAP;
    
    class Drawable
    {
    	public:
    	virtual void draw(BITMAP* b) =0;
    };
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    14

    aaah

    Thanks. But still, that's such a weird, ambiguous error...

    I'm going to include my library.h file then.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    The forward declaration should be enough there, and a forward declaration is generally a better idea than including an entire header.

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    14

    how?

    How would you use a forward declaration here? You can't just say "struct BITMAP" like he did, can you?

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Why not?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by pandu View Post
    How would you use a forward declaration here? You can't just say "struct BITMAP" like he did, can you?
    Yes, you can - you do need a semicolon on the end. As long as you don't need to know the content of the struct/class, it's fine to forward declare it by just saying "struct aname;" or "class somename;".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Jun 2008
    Posts
    14
    What? Even without the braces? Never read of it in any tutorial. Lol, so many surprises in this language.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by pandu View Post
    What? Even without the braces? Never read of it in any tutorial. Lol, so many surprises in this language.
    Yes, definitely without braces.

    And it's a way to tell the compiler "there is a class[1] of this name" without actually defining what it contains. It has (at least) two potential uses:
    1. You want to return a pointer or reference to a class, but you don't actually want to tell the code receiving this class what is in the class - a HANDLE in windows is a typical example where the application is given a pointer to something without actually knowing what the data it points at is.
    2. You need one class to hold a reference or pointer to another, where the other class also holds a reference or pointer to the first class (e.g. a parent/child relationship).

    [1] When I say class, it is, for the purposes of this discussion, interchangeable with struct.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  2. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM

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