Classes

This is a discussion on Classes within the C++ Programming forums, part of the General Programming Boards category; Code: class painter { virtual void paint(); }; painter p; p->paint(); This wouldn't work. :/ Could you tell me why? ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    141

    Classes

    Code:
    class painter {
    
    virtual void paint();
    
    };
    
    
    
    painter p;
    p->paint();
    This wouldn't work. :/ Could you tell me why?

    Errors:
    Code:
    error C2143: syntax error : missing ';' before '->'
    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    error C2371: 'p' : redefinition; different basic types
    1>  see declaration of 'p'

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    for p-> to work, p must be either a pointer or have the operator -> defined for it.
    You should probably look for some tutorials along the lines of virtual functions.
    What are you trying to accomplish?
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    141
    I tried a pointer. Im trying to make an object, painter is just a name I used.

    Virtual they use is used to make objects output.

    Also, what do you mean by operator -> defined for it?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by bobbelPoP View Post
    I tried a pointer. Im trying to make an object, painter is just a name I used.
    If you had tried a pointer, it would have worked.

    Quote Originally Posted by bobbelPoP View Post
    Virtual they use is used to make objects output.
    This is so amazingly wrong as to boggle the mind. The word virtual is used to indicate that a derived class may override the paint method, nothing more.

    Quote Originally Posted by bobbelPoP View Post
    Also, what do you mean by operator -> defined for it?
    Just what it says. C++ only defines -> to work on pointers to structures or classes; but if you make your own class you can define -> on that class too. (You shouldn't, probably, but you can.)

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by tabstop View Post
    If you had tried a pointer, it would have worked.


    This is so amazingly wrong as to boggle the mind. The word virtual is used to indicate that a derived class may override the paint method, nothing more.
    1. Well it didn't
    2. They SAID the concept was that.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Oh you/we forgot one other thing:
    Code:
    #include <iostream>
    
    class painter {
        public:
        virtual void paint();
    
    };
    
    void painter::paint() {
        std::cout << "Works.\n";
    }
    
    int main() {
        painter p;  //alternatively painter *p;
        p.paint();  //alternatively p->paint();
        return 0;
    }
    Edit to add: I wonder who said that virtual was used for output, and what they meant by it.
    Last edited by tabstop; 07-15-2008 at 09:58 PM.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    141
    I swore I added that, must have accidently removed it.

  8. #8
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    alright, throwing in my two cents to reconcile the obvious:

    they probably said that virtual is used to output the correct data when paint is called in descendants of painter.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by bobbelPoP View Post
    1. Well it didn't
    2. They SAID the concept was that.
    Who is "They"?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

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