Class Troubles

This is a discussion on Class Troubles within the C++ Programming forums, part of the General Programming Boards category; Hey I have been just trialing some classes to use within a game for an assignment and it is coming ...

  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()
    Attached Files Attached Files

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,954
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,954
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,954
    Right, so where did you define (implement) Cat()?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,954
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,954
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,954
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

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