OVH Cloud OVH Cloud

Où placer les const

4 réponses
Avatar
Benoit Rousseau
Bonjour,

J'ai une fonction qui retourne une list qui ne doit pas être modifiée :
const list<Robot*>& make_team( );
La liste est constante, mais les robots ne le sont pas (?)

je récupère cette liste et la passe en argument à une autre fonction qui
fait l'union de toutes les equipes :

void
RobotController::add_robots_in_game( const list<Robot*>& robot_list ) {
list<Robot*>::iterator li;
for( li = robot_list.begin(); li != robot_list.end(); li ++ )
all_robots_in_game.push_back( *li );
}

Je recois l'erreur suivante :
conversion from
`std::_List_iterator<Robot*, Robot* const&, Robot* const*>'
to non-scalar type
`std::_List_iterator<Robot*, Robot*&, Robot**>' requested

Où est l'erreur ? où doivent se placer les const ? ...




--------------------------------------------
Benoît Rousseau : roussebe at spray dot se
Jouez en programmant : http://realtimebattle.sourceforge.net/

4 réponses

Avatar
Benoit Dejean
Le Sat, 25 Oct 2003 12:33:17 +0200, Benoit Rousseau a écrit :


void
RobotController::add_robots_in_game( const list<Robot*>& robot_list ) {
list<Robot*>::iterator li;
for( li = robot_list.begin(); li != robot_list.end(); li ++ )
all_robots_in_game.push_back( *li );
}

Je recois l'erreur suivante :
conversion from
`std::_List_iterator<Robot*, Robot* const&, Robot* const*>' to
non-scalar type
`std::_List_iterator<Robot*, Robot*&, Robot**>' requested

Où est l'erreur ? où doivent se placer les const ? ...


list<Robot*>::const_iterator li;


Avatar
Ivan Vecerina
"Benoit Dejean" wrote in message
news:


void
RobotController::add_robots_in_game( const list<Robot*>& robot_list ) {
list<Robot*>::iterator li;
for( li = robot_list.begin(); li != robot_list.end(); li ++ )
all_robots_in_game.push_back( *li );
}
...


list<Robot*>::const_iterator li;


Correct.
Ceci dit, la fonction pourrait s'écrire plus simplement:
all_robots_in_game.insert( all_robots_in_game.end()
robot_list.begin(), robot_list.end() );
ou:
std::copy( robot_list.begin(), robot_list.end()
, std::back_inserter( all_robots_in_game ) );


Suggestion utile, je l'espère...
Ivan
--
http://ivan.vecerina.com
http://www.brainbench.com <> Brainbench MVP for C++


Avatar
Benoit Rousseau
Ivan Vecerina wrote:
"Benoit Dejean" wrote in message
void
RobotController::add_robots_in_game( const list<Robot*>& robot_list ) {
list<Robot*>::iterator li;
for( li = robot_list.begin(); li != robot_list.end(); li ++ )
all_robots_in_game.push_back( *li );
}
list<Robot*>::const_iterator li;



Correct.
Ceci dit, la fonction pourrait s'écrire plus simplement:
all_robots_in_game.insert( all_robots_in_game.end()
robot_list.begin(), robot_list.end() );
ou:
std::copy( robot_list.begin(), robot_list.end()
, std::back_inserter( all_robots_in_game ) );
Suggestion utile, je l'espère...


Oui, c'est en effet plus joli...

--
--------------------------------------------
Benoît Rousseau : roussebe at spray dot se
Jouez en programmant : http://realtimebattle.sourceforge.net/



Avatar
Benoit Dejean
Le Sun, 26 Oct 2003 02:08:07 +0200, Ivan Vecerina a écrit :

"Benoit Dejean" wrote in message

list<Robot*>::const_iterator li;


Correct.
Ceci dit, la fonction pourrait s'écrire plus simplement:
all_robots_in_game.insert
ou:
std::copy(... std::back_inserter );


Suggestion utile, je l'espère...


certes
ça m'apprendra à flemmarder :oD