Thread: Help with classes and inheritance!!

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    4

    Help with classes and inheritance!!

    I have this program I am doing for a class and I keep on getting the same error on the way my functions are being called.


    45 C:\Dev-Cpp\ no matching function for call to `circus::getanimal()'
    note C:\Dev-Cpp\:15 candidates are: void circus::getanimal(std::string, std::string, int)
    46 C:\Dev-Cpp\ no matching function for call to `tiger::getstripes()'
    note C:\Dev-Cpp\REDOCIRCUS.cpp:26 candidates are: void tiger::getstripes(std::string, int)

    and so on..

    any help would be greatly appreciated


    Code:
    #include <iostream> 
    #include <iomanip>
    #include <cstring>
    
    using namespace std;
    
    class circus 
    {
          public: 
                  string animaltype;
                  string animalname;
                  int age;
                  int dailyfood;
                  
                  void getanimal(string animalname, string animaltype,int animalage);
                  void getfood();
                  };
    class tiger : public circus
    {
          public:
                 int stripes;
                 int toothlength;
                 char maneater;
                 int jumpdistance;
    private:
            void getstripes(string animalname, int stripes);
                 };
    class elephant : public circus
    {
          public:
                 int tusks;
                 string eletype;
                 int num;
    private: 
            void gettype(int tusks,int num, string eletype, string animalname);
                 };         
    
    circus animals;
    tiger cat;
    elephant grey;
    int main()
    {
    
     cout << "Hello and welcome to the super fantastic circus!" << endl << "Please enter the following information by pressing enter after each input" << endl;
    
     animals.getanimal();
     cat.getstripes();
     grey.gettype();
        
        
        system("pause");
        return 0;
    }
    
    void circus::getanimal(string animalname, string animaltype,int animalage) 
    {
         cout << "What is your animal's name?" << endl;
         cin >> animalname;
         cout << "what kind of animal is" << animalname << "?" << endl;
         cin >> animaltype;
         cout << "what is " << animalname << "'s" << "age?" << endl;
         cin >> animalage;
         cout << animalname << "is a" << animaltype << "and is" << animalage << "years old." << endl;
         }
         
    void circus::getfood()
    {
         cout << "Please enter the animal's name." << endl;
         cin >> animalname;
         cout << "Please enter the amout of food" << animalname << "eats" << endl;
         cin >> dailyfood;
         cout << animalname << "eats" << (dailyfood * 7) << "in one week" << endl;
         }
         
    void tiger::getstripes(string animalname, int stripes)
    {
         cout << "Please enter the name of the tiger." << endl;
         cin >> animalname;
         cout << "Please enter the number of stripes" << animalname << "has." << endl;
         cin >> stripes;
         cout << "So" << animalname << "has" << stripes << endl;
         }
    
    void elephant::gettype(int tusks,int num, string eletype, string animalname)
    {
         cout << "What type of elephant is " << animalname << "?" << "Type 1 for African and 2 for Asian" << endl;
         cin >> num;
         if (num == 1)
         (eletype = "African");
         else
         (eletype = "Asian");
         cout << "Does " << animalname << "have tusks type 1 for yes and 2 for no" << endl;
         cin >> tusks;
         if (tusks == 1)
         cout << animalname << " is a " << eletype << " elephant and does have tusks " << endl;
         else
         cout << animalname << " is a " << eletype << " elephant and does not have any tusks " << endl;
              }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    45 C:\Dev-Cpp\ no matching function for call to `circus::getanimal()'
    note C:\Dev-Cpp\:15 candidates are: void circus::getanimal(std::string, std::string, int)
    Simple enough to understand what it's saying...
    Code:
    class circus 
    {
       ...
                  
       void getanimal(string animalname, string animaltype,int animalage);
       ...
    };
    ...
    
    circus animals;
    ...
    
    int main()
    {
        cout << "Hello and welcome to the super fantastic circus!" << endl
             << "Please enter the following information by pressing enter after each input" << endl;
    
        animals.getanimal();
        ...
    
        return 0;
    }
    
    void circus::getanimal(string animalname, string animaltype,int animalage) 
    {
        cout << "What is your animal's name?" << endl;
        cin >> animalname;
        cout << "what kind of animal is" << animalname << "?" << endl;
        cin >> animaltype;
        cout << "what is " << animalname << "'s" << "age?" << endl;
        cin >> animalage;
        cout << animalname << "is a" << animaltype << "and is" << animalage << "years old." << endl;
    }

    46 C:\Dev-Cpp\ no matching function for call to `tiger::getstripes()'
    note C:\Dev-Cpp\REDOCIRCUS.cpp:26 candidates are: void tiger::getstripes(std::string, int)
    And same thing again...
    Code:
    class tiger : public circus
    {
        ...
        void getstripes(string animalname, int stripes);
    };
    
    ...
    tiger cat;
    ...
    int main()
    {
        ...
        cat.getstripes();
        ...
    }
    ...
    void tiger::getstripes(string animalname, int stripes)
    {
         cout << "Please enter the name of the tiger." << endl;
         cin >> animalname;
         cout << "Please enter the number of stripes" << animalname << "has." << endl;
         cin >> stripes;
         cout << "So" << animalname << "has" << stripes << endl;
    }
    You're saying that your functions take arguments but when you actually call them in your code, you don't pass any arguments into the functions. BTW, I don't think you need arguments passed into these particular functions.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    Thank you, I have seemed to fix that error but now I have a new error. It seems to not like the fact I am using private functions but its part of the assignment. It compiles and runs if I take the private out. this is my current code.

    Code:
       #include <iostream> 
    #include <iomanip>
    #include <cstring>
    
    using namespace std;
    
    class circus 
    {
          public: 
                  string animaltype;
                  string animalname;
                  int animalage;
                  int dailyfood;
                  
                  void getanimal();
                  void getfood();
                  };
    class tiger : public circus
    {
          public:
                 int stripes;
                 int toothlength;
                 char maneater;
                 int jumpdistance;
          private:
            void getstripes();
                 };
    class elephant : public circus
    {
          public:
                 int tusks;
                 string eletype;
                 int num;
          private:
            void gettype();
                 };         
    
    circus animals;
    tiger cat;
    elephant grey;
    int main()
    {
        
    char num;
     cout << "Hello and welcome to the super fantastic circus!" << endl << "Please enter the following information by pressing enter after each input" << endl;
     do{
     animals.getanimal();
     cout << "Please selected what you would like to do next." << "type 1 if chose a tiger and 2 if you chose a elephant and 3 if you didnt choose either and would like to choose one.";
     cin >> num;
     switch (num)
    {
     case '1':
     {
     cat.getstripes();
     cat.getfood();
     }
          break;
     case '2':
     {
     grey.gettype();
     grey.getfood();
     }
          break;
    }
         }while(num != 3);
        
        system("pause");
        return 0;
    }
    
    void circus::getanimal() 
    {
         cout << "What is your animal's name?" << endl;
         cin >> animalname;
         cout << "what kind of animal is" << animalname << "?" << endl;
         cin >> animaltype;
         cout << "what is " << animalname << "'s" << "age?" << endl;
         cin >> animalage;
         cout << animalname << "is a" << animaltype << "and is" << animalage << "years old." << endl;
         }
         
    void circus::getfood()
    {
         cout << "Please enter the amout of food" << animalname << "eats" << endl;
         cin >> dailyfood;
         cout << animalname << "eats" << (dailyfood * 7) << "in one week" << endl;
         }
         
    void tiger::getstripes()
    {
         cout << "Please enter the number of stripes" << animalname << "has." << endl;
         cin >> stripes;
         cout << "So" << animalname << "has" << stripes << endl;
         }
    
    void elephant::gettype()
    {
         cout << "What type of elephant is " << animalname << "?" << "Type 1 for African and 2 for Asian" << endl;
         cin >> num;
         if (num == 1)
         (eletype = "African");
         else
         (eletype = "Asian");
         cout << "Does " << animalname << "have tusks type 1 for yes and 2 for no" << endl;
         cin >> tusks;
         if (tusks == 1)
         cout << animalname << " is a " << eletype << " elephant and does have tusks " << endl;
         else
         cout << animalname << " is a " << eletype << " elephant and does not have any tusks " << endl;
              }
    sorry the errors are :
    26 C:\Dev-Cpp\REDOCIRCUS.cpp `void tiger::getstripes()' is private
    54 C:\Dev-Cpp\REDOCIRCUS.cpp within this context
    35 C:\Dev-Cpp\REDOCIRCUS.cpp `void elephant::gettype()' is private
    60 C:\Dev-Cpp\REDOCIRCUS.cpp within this context

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Is this design from your teacher? It seems rather odd to me. Derived classes should be an example of an is-a relationship, and a tiger is certainly not a circus. A circus should contain animals like tigers and elephants.

  5. #5
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Yes sure, it should generate an error as you are trying to access private members from outside the class. Make your instances(variables) as private and member functions as public. But i'll recommend you to read classes first.
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    While you're at it, it's a good idea to learn how to properly indent your code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    I seemed to have fixed that but I am now having trouble with my strings. When I try to output them in my derived classes it just gives me an empty space. Does this mean my derived classes are not inheriting anything form the base class??

    Code:
    void tiger::getstripes()
    {
         cout << "Please enter the number of stripes " << animalname << " has." << endl;
         cin >> stripes;
         cout << "So " << animalname << " has" << stripes << endl;
         }
    for the output of I just get "Please enter the number of stripes has"
    Last edited by alacahzam; 03-18-2011 at 07:40 AM.

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    You get a blank because when you instansiate your subclass objects a new base class object is also created, so you are probably referencing the empty string in a base class object somewhere,Animalname is probably outputiing but is just an empty string Try initialising the animal names to something default in a constructor, you will soon follow what is going on
    Last edited by rogster001; 03-18-2011 at 08:36 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    I tried to add a constructor and it didn't work out. is there any other way to get my user to define the object and have it be used throughout the program or is it necessary to use a constructor?

  10. #10
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    constructor

    the constructor was not meant t be the fix though perhaps it could be... it was just a way to let you giva a value to your members on instantiation, so you could see what happens. You can use staic member variables where required in the base class to fix problem
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Defining classes following user input. Inheritance.
    By Swerve in forum C++ Programming
    Replies: 7
    Last Post: 03-17-2009, 09:21 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. inheritance from classes own members?
    By atapi103 in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 10:19 PM
  4. Inheritance Classes w/ same function name
    By Diamonds in forum C++ Programming
    Replies: 3
    Last Post: 03-11-2003, 01:11 AM
  5. Classes & Inheritance
    By TankCDR in forum C++ Programming
    Replies: 5
    Last Post: 03-09-2002, 06:25 PM

Tags for this Thread