Thread: Virtual Functions

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    48

    Virtual Functions

    IM getting an error, and have no idea what the cause is.. Im creating a virtual class function and its driving me nuts..

    I get the following:
    Code:
    square.cxx:6: error: virtual outside class declaration
    square.cxx:8: error: virtual outside class declaration
    square.cxx:10: error: virtual outside class declaration
    square.cxx:13: error: virtual outside class declaration
    Here is my square class:

    Code:
    class square {
       public:
          square();
          virtual ~square();
          virtual void read(istream &in);
          virtual void display() const;
    
       protected:
          string name[30];
    
    };
    and here is code.cxx

    Code:
    square::square(){};
    
    virtual square::~square(){};
    
    virtual void square::read(istream &in) {
       cout << "test\n";
    }
    virtual void square::display() const {}
    I just dont understand how I am using "virtual" outside of the class????

    or what the compiler is complaining about....

    thanks for any assistance/explanation...

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Leave the 'virtual' off of the definition (that is, it should only appear once, and that place is inside the class where the function is declared).

    *edit*
    More specifically, like this:
    Code:
     class A
     {
        virtual void foo();
     };
     
     void A::foo()
     {
     }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    48
    haha... your a lifesaver... or in my case I should say you just saved me from becoming bald 10 yrs early :-).

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    the same applies for the static (vars and functions), and friend keywords, as far as can recall now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i need a good example of virtual functions
    By Anddos in forum C++ Programming
    Replies: 10
    Last Post: 02-15-2006, 11:48 AM
  2. Thread creation on virtual functions
    By gustavosserra in forum C++ Programming
    Replies: 13
    Last Post: 10-14-2004, 08:03 AM
  3. recursive virtual functions
    By ygfperson in forum C++ Programming
    Replies: 0
    Last Post: 05-25-2003, 08:00 PM
  4. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM