Thread: Class Troubles

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128

    Thumbs down Class Troubles

    Hey I have been just trialing some classes to use within a game for an assignment and it is coming up with a error in source.cpp line 137 and later.

    It says somethign about it not being a part for the class () () and I have jumbled stuff around and followed some books but still no luck...

    was just wondering if anyone can spot the problem.

    I uploaded both problems and the call functions are commented out .

    Espeon.Meow()
    Ruffles.Woof()
    Eskia.Screech()

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem is that these lines declare functions, not objects:
    Code:
    Cat Espeon();
    
    Dog Ruffles();
    
    Eagle Eskia();
    You should have written:
    Code:
    Cat Espeon;
    
    Dog Ruffles;
    
    Eagle Eskia;
    Oh, and avoid global variables and do not write such long functions.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Ahhh thanks kindly

    Not sure how to shorten certain functions but Ill try.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    I did that and it had a compile error,

    undefined reference of Class::Cat()

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In other words, you declared the default constructor but did not define it (or maybe you declared some other constructor but did not also define the default constructor).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Code:
    class Cat                                                          // Begins declaration of class
    {                                                                  //
        public:                                                        // Begins public section
            Cat();                                                     //
            ~Cat();                                                    //
            void Meow()
            {cout << "Meow! \n";}                                      // General functions and so fourth
            void Info();
        private:
    
    };
    That is my class header code for just the cat to start with.

    Code:
    Cat Espeon;
    
        switch(petChoice)   // Depends on the pet of the player, displays a different pet introduction.
    {
            case 1:
    
            Espeon.Meow();
            cout << "Espeon slips into the stable, rubs up against your horses leg and jumps into you saddle pack.\n\n";
            Espeon.Meow();
            break;
    and that is basicly the code for the cat in the source code

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Right, so where did you define (implement) Cat()?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    You mean where I defined the cat Espeon? (sorry im new to terminology and c ++)
    if so, at the top. The rest is in the header file

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Aliaks
    You mean where I defined the cat Espeon?
    No, I mean the place where you implemented the default constructor that you declared for the Cat class. Presumably this was in a source file.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    from my knowledge i defined it in the header file under the cat() which was ~cat() but as im not 100% sure of how it works yet I probably did ity wrong

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Aliaks
    from my knowledge i defined it in the header file under the cat() which was ~cat()
    ~Cat() declares the destructor.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    yeah I have that in the header

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay, it sounds like you are not getting it at all. Change your class definition to:
    Code:
    class Cat
    {
    public:
        Cat() {}
    
        void Meow()
        {
            cout << "Meow! \n";
        }
    
        void Info() {}
    private:
    
    };
    Do you still get that error? Can you see that I defined the default constructor whereas you only declared it?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Ahhh yep I see now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM