TICPP and const issues

This is a discussion on TICPP and const issues within the C++ Programming forums, part of the General Programming Boards category; I'm getting this error: Game.cpp:416: error: passing ‘const Coach’ as ‘this’ argument of ‘const std::string Person::getName()’ discards qualifiers Code: ticpp::Element ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    44

    TICPP and const issues

    I'm getting this error:
    Game.cpp:416: error: passing ‘const Coach’ as ‘this’ argument of ‘const std::string Person::getName()’ discards qualifiers
    Code:
    ticpp::Element Game::toXML() const {
    
      Team* home=homeTeam;
      Team* away=awayTeam;
    
      ticpp::Element game=("Game");
      game.SetAttribute("location",location);
    
      //Home Team Elements
      ticpp::Element homeTeamElement("HomeTeam");
      ticpp::Element homeCoach=("Coach");
      
    
      //Coach Elements
      //Coach Attributes
    
      //ERROR IS HERE
      homeCoach.SetAttribute("name", (home->getCoach())->getName());
    
      homeTeamElement.InsertEndChild(homeCoach);
    home is a pointer, getCoach() returns a const Coach* const, and getName() returns a const string.

    If I remove all the consts, it works fine, but I need the consts.

    Any help would be great.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Can you declare getName as a const function? (The name suggests you can.)

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    44
    Code:
     const string Person::getName(){
    	const holder=name;
    	return holder;
    }

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    44
    Code:
    const string Person::getName() const{
    	const holder=name;
    	return holder;
    }
    This works. I thought the const after the function dealt with the pointer, not the function itself.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by TIMBERings View Post
    Code:
    const string Person::getName() const{
    	const holder=name;
    	return holder;
    }
    This works. I thought the const after the function dealt with the pointer, not the function itself.
    You don't really have a pointer (unless you mean the this pointer?) here at all, so I have no idea where you were headed. (And if you do mean the this pointer, then you are right in practice at least -- putting a const there means the this pointer can be a const pointer.)

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    44
    I know there's not a pointer in this one. That's why I hadn't put const at the end of the declaration, because I thought it only applied to pointers.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    44
    While we're at it, along the same lines:

    Team.cpp:47: error: passing ‘const std::map<int, Player, std::less<int>, std::allocator<std::pair<const int, Player> > >’ as ‘this’ argument of ‘_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = int, _Tp = Player, _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, Player> >]’ discards qualifiers
    Code:
    map<int, Player> players;
    
    const Player* Team::getPlayer(int number) const{
      
      map<int,Player>::const_iterator it;
      it=players.find(number);
      if (it!=players.end()){
    
        //ERROR HERE
        const Player* const playerPtr=&players[number];
        
        return playerPtr;
      }
      else {
        return NULL;
      }
    }
    I can't declare player as a const because I need to add to it later.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I take it players is a member of Team? If so, then you can't use [], since that's not a const function/operator (since you use [] to add to a map). On the other hand, I don't know that you need to go to all that trouble, since it points to the element you want; why not
    Code:
    return &(*it);
    ?

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    44
    I finished with this, and it compiled. Now I just hope it gets me what I want;
    Code:
    const Player* Team::getPlayer(int number) const{
      map<int,Player>::const_iterator it;
      it=players.find(number);
      if (it!=players.end()){
        const Player* const playerPtr=&((*it).second);
        return playerPtr;
      }
      else {
        return NULL;
      }
    }
    Thank you.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,993
    I suggest that you write it in this way instead:
    Code:
    const Player* Team::getPlayer(int number) const {
        map<int,Player>::const_iterator it = players.find(number);
        if (it != players.end()) {
            return &it->second;
        }
        else {
            return NULL;
        }
    }
    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

Popular pages Recent additions subscribe to a feed

Tags for this Thread


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