classes+bool

This is a discussion on classes+bool within the C++ Programming forums, part of the General Programming Boards category; Hi guys, i ran into a problem in my simple class definition below which rendered me handicapped for hours... mainly, ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    29

    Unhappy classes+bool

    Hi guys, i ran into a problem in my simple class definition below which rendered me handicapped for hours...

    mainly, if i commented out the 2 lines of string input, the program works, but if not, there is a compilation error which i can't debug. any expert knows why?
    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <queue>
    
    #include <vector>
    
    using namespace std;
    
    class Contestant {
    public:
    Contestant() //can initialise the class without an int i. copy constructor?
    {
    }
    Contestant(int i) 
    {
        weight = i;
    }
    string getFirstName() 
    { //accessor
      return first_name;
    }
    void setFirstName(string name) 
    { //mutator
      first_name = name;
    }
    string getLastName() 
    { //accessor
      return last_name;
    }
    void setLastName(string surname) 
    { //mutator
      last_name = surname;
    } 
    int getHeight() 
    { //accessor
      return height;
    }
    void setHeight(int ht) 
    { //mutator
      height = ht;
    } 
    int getWeight() 
    { //accessor
      return weight;
    }
    void setWeight(int wt) 
    { //mutator
      weight = wt;
    } 
    
    private:
    string first_name;
    string last_name;
    int height;
    int weight;
    };
    
    //compares weight
    bool lighter( Contestant weight1 , Contestant weight2 ) { 
    return (weight1.getWeight() < weight2.getWeight()) ;}
    
    bool heavier( Contestant weight1 , Contestant weight2 ) { 
    return (weight1.getWeight() > weight2.getWeight());}
    
    int main(){
    
    vector<Contestant> v_data;
    string personaldata;
    int each_data;
    Contestant c(each_data); //create an object of contestant class
       
    cout << "Enter first name,last name,height (int),weight(int)." << endl;
    
    while (getline(cin,personaldata)){
          istringstream is(personaldata);
          
    Contestant contestantTemp;
    //while (is >> each_data){
            
      is >> each_data;
      // Set first name
      contestantTemp.setFirstName(each_data); // what's wrong with this line? invalid conversion from 'int' to 'const char'
    
      is >> each_data;
      // Set last name
      contestantTemp.setLastName(each_data); // what's wrong with this line? invalid conversion from 'int' to 'const char'
    
    
      is >> each_data;
      // Set height
      contestantTemp.setHeight(each_data);
      is >> each_data;
      // Set weight
      contestantTemp.setWeight(each_data);
      
        v_data.push_back(contestantTemp);
    }
    
    vector<Contestant>::iterator i = v_data.begin();
    vector<Contestant>::iterator e = v_data.end();
    
    sort (i, e, lighter ); // use bigger for descending
    cout<< "In ascending order of weight:" << endl;
    for (; i != e; ++i)  cout<< i ->getWeight() << endl;
    
    system("pause");
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    You have each_data declared as an int, but you are passing it to a function that expects a string.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >  is >> each_data;
    >  // Set first name
    >  contestantTemp.setFirstName(each_data); // what's wrong with this line? invalid conversion from 'int' to 'const char'
    each_data is a int. You want to read into a string. So declare a temporary string variable. For example:
    Code:
      string first;
      is >> first;
      // Set first name
      contestantTemp.setFirstName(first); // what's wrong with this line? invalid conversion from 'int' to 'const char'

  4. #4
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Here you declared each_data as an int:
    Code:
    int each_data;
    And here you pass that int to setFirstName().
    Code:
    contestantTemp.setFirstName(each_data);
    But here your function is expecting a string, not an int.
    Code:
    void setFirstName(string name)
    As the compiler said, invalid conversion from 'int' to 'const char' (string).

    This would work though:
    Code:
    contestantTemp.setFirstName((string)each_data);
    EDIT:

    Oh sicko, 2 people posted while I was writing this up!!!
    But my writeup is a lot more fancy.
    Last edited by Queatrix; 10-11-2006 at 11:51 AM.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Queatrix
    This would work though:
    Code:
    contestantTemp.setFirstName((string)each_data);
    Define work.

  6. #6
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    I will only guarantee successful compilation.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Queatrix
    I will only guarantee successful compilation.
    In that case, I'll shut up.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    29
    thank you, daved, swoopy and queatrix. i understood the function 'is >> __' more and also had a successful compilation. but somehow i couldn't manage to cast the each_data into a string.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Do not cast each_data into a string. Create a separate string variable for reading in string input and use that instead of each_data when the input is a string.

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    29

    seperate string

    thank you that solution worked perfectly well. if i had an optimal height and weight. how do i compare the 4 different variables together with a boolean function and sort using STL? bcos i figure out can only compare 2 items at once.

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,804
    These should be const reference arguments:
    Code:
    //compares weight
    bool lighter( const Contestant& weight1 , const Contestant& weight2 ) { 
    return (weight1.getWeight() < weight2.getWeight()) ;}
    
    bool heavier( const Contestant& weight1 , const Contestant& weight2 ) { 
    return (weight1.getWeight() > weight2.getWeight());}
    All your "get" member functions should be const functions:
    Code:
    string getFirstName() const
    { //accessor
      return first_name;
    }
    
    string getLastName() const
    { //accessor
      return last_name;
    }
    
    int getHeight() const
    { //accessor
      return height;
    }
    
    int getWeight() const
    { //accessor
      return weight;
    }
    Quote Originally Posted by wind_lark
    how do i compare the 4 different variables together with a boolean function and sort using STL? bcos i figure out can only compare 2 items at once.
    You can have functions, like lighter and heavier, but that operate on all the data members of the class you need in order to sort to your desires. Say you wanted to sort by last name and then first name, you could do this.

    Code:
    struct sort_by_name
    {
    public:
        bool inline operator()(const Contestant& contestant1, const Contestant& contestant2 )
        {
            if( contestant1.getLastName() < contestant2.getLastName() )
                return true;
            else if( contestant1.getLastName() == contestant2.getLastName() )
                return contestant1.getFirstName() < contestant2.getFirstName();
            else return false;
        }
    };
    
    ...
    
    sort( v_data.begin(), v_data.end(), sort_by_name() );
    "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

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    29

    Thumbs up struct within classes?

    thanks for ur wonderful insight, hk_mp5kpdw, sorry for the late reply. I'm not sure how to incorporate struct into a class, but i did get some clues on how to formulate the algorithm..will work on it. thanks.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    The struct in this case does not have to be inside the class, it can be next to it, or just in the source file (preferably inside an unnamed namespace). If you want, you could nest it inside the class, the code would be the same except that you would have to use Contestant::sort_by_name if you sorted the contestants in a function that is not part of that class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing params between managed c++ and unmanaged c++
    By cechen in forum C++ Programming
    Replies: 11
    Last Post: 02-03-2009, 07:46 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  5. Need Help With an RPG Style Game
    By JayDog in forum Game Programming
    Replies: 6
    Last Post: 03-30-2003, 07:43 PM

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