Problem using char's in classes.

This is a discussion on Problem using char's in classes. within the C++ Programming forums, part of the General Programming Boards category; This isn't working for some reason: Code: char Cat::SetAge(char age) { itsAge = age; } char Cat::GetAge() { return itsAge; ...

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Question Problem using char's in classes.

    This isn't working for some reason:
    Code:
    char Cat::SetAge(char age)
    
       {
    
        itsAge = age;
        }
    char Cat::GetAge()
    
        {
    
        return itsAge;
        }
    18 C:\Documents and Settings\Owner\My Documents\Untitled1.cpp
    prototype for ` candidate is C:\Documents and Settings\Owner\My Documents\Untitled1.cpp:8
    int


    I am out of ideas to what is wrong.
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    OS/Compiler?

    Anyway, you're not returning a value from SetAge()
    Away.

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    You'll have to post the whole code. The only thing I see wrong is that your first function doesn't return anything even though you have it defined as returning a char. My guess is that you meant for that function to be void. But most compilers would still compile that, just give you a warning.

    Also, shouldn't the cats age be an integer and not a char?

  4. #4
    1479
    Join Date
    Aug 2003
    Posts
    253
    Code:
    class Cat
        {
        public:
            int GetAge();
            int SetAge(char age);
            int SetWeight(int weight);
            int GetWeight();
            void Meow();
        private:
            int itsAge;
            int itsWeight;
        };
    //=========setting up the age=============//    
    char Cat::SetAge(char age)
        {
        itsAge = age;
        }
    char Cat::GetAge()
        {
        return itsAge;
        }
    
    //===============Ending the age==============//
    //==============Setting up its Weight=========//
    int Cat::GetWeight()
        {
        return itsWeight;
        }
    int Cat::SetWeight(int weight)
        {
       itsWeight=weight;
        }
    //==============Ending its weight===============//
    
    void Cat::Meow()
        {
        cout <<"Meow" <<endl;
        }
    widows xp dev-c++ 4.9.8.0
    Knowledge is power and I want it all

    -0RealityFusion0-

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Its because you have your functions declared differently inside the class and outside the class. Inside they are all returning ints, outside some are returning chars. Make sure that every function that returns NOTHING is void, not int or char or whatever. Also make sure that your functions are written EXACTLY the same inside and outside the function, otherwise the compiler will view them as different and will complain that it can't find the function it is looking for.

  6. #6
    1479
    Join Date
    Aug 2003
    Posts
    253
    NvM I figured it out!
    Knowledge is power and I want it all

    -0RealityFusion0-

  7. #7
    1479
    Join Date
    Aug 2003
    Posts
    253
    Ok, now I am trying to make user input be a chacater string in my class. And I think this is how I would do it:

    How I declared it in class:
    Code:
    class Cat
        {
        public:
            char GetAge();
            char SetAge(char age[20]);
    This is how I defined it:
    Code:
    char Cat::SetAge(char age[20])
        {
        itsAge = age[20];
        }
    This is how I called it in main():
    Code:
    char age[20];      
            Cat Frisky;
            cout <<"Enter age of Cat." <<endl;
            cin.getline (age, 20);
            Frisky.SetAge(age[20]);
            cout <<Frisky.GetAge();
            cout <<endl;
    But I get an error message:
    51 C:\Documents and Settings\Owner\My Documents\Untitled1.cpp
    invalid initialization
    Knowledge is power and I want it all

    -0RealityFusion0-

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>itsAge = age[20];
    Nope.

    sscanf( age, "%d", &itsAge );
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    class Cat
    {
        public:
            char GetName();
            void SetName(char * name_);
        private:
            char name[20];
    }; 
    
    This is how I defined it:
    
    void Cat::SetName(char * name_)
        {
           strcpy(name, name_);
        }
    
    This is how I called it in main():
    
    char name_[20];      
            Cat Frisky;
            cout <<"Enter age of Cat." <<endl;
            cin.getline (name_, 20);
            Frisky.SetName(name_);
            cout <<Frisky.GetName();
            cout <<endl;
    Last edited by elad; 08-15-2003 at 11:53 AM.

  10. #10
    1479
    Join Date
    Aug 2003
    Posts
    253
    Originally posted by XSquared
    sscanf( age, "%d", &itsAge ); [/B]
    What does that do and mean?
    Knowledge is power and I want it all

    -0RealityFusion0-

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    It's C code that says: take the integer stored at the address of itsAge and store it in the null-terminated char array called age---if I remember my C correctly

    Edit:
    looked it up:

    sscanf reads from the C style string (first parameter)and sends that value to the target (third parameter) using the format of the second parameter, similar to atoi() atof() and atol() accept the third argument can be char, etc. as well as a numerical type.

    sprintf changes the value of the third parameter which is of the type of the second parameter into the first first argument which is a C style string.
    Last edited by elad; 08-16-2003 at 10:32 AM.

  12. #12
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Right. And the "%d" is a format specifier which says take the first argument (which is an integer) and put it in the string.

    Try using std::string in <string> header instead. They are easier to use, and don't leak memory or have buffer overrun errors.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Having a problem with templates and classes
    By bowluswj in forum C++ Programming
    Replies: 4
    Last Post: 06-26-2007, 12:55 PM
  3. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 12:30 PM
  4. Problem with character arrays in classes
    By spoketoosoon in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2004, 02:57 AM
  5. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM

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