2016-01-11 5 views
1

Je cette fonction qui reçoit un const vector de <unsigned int, unsigned int> paires:C++ erreur stringstream: aucune correspondance pour l'opérateur << pour les types d'opérandes std :: string et const unsigned int

// Type definitions 
typedef std::pair<unsigned int, unsigned int> IdsPair; 
typedef std::vector<IdsPair> IdsPairList; 
typedef IdsPairList::const_iterator IdsPairIterator; 

void Foo(const IdsPairList myPairsList){ 
    std::stringstream ss; 
    ss << "List of ids: ("; 
    for(IdsPairIterator idPair = myPairsList.begin(); 
     idPair != myPairsList.end(); idPair++){ 
     ss << (idPair->first == myPairsList.begin()) ? "" : "," << idPair->first; // ERROR 
    } 
    ss << ")"; 
    std::string query = ss.str(); 
} 

A la ligne sélectionnée avec // ERROR, Je reçois l'erreur suivante:

error: no match for ‘operator<<’ (operand types are ‘std::string {aka std::basic_string<char>}’ and ‘const unsigned int’) 

Je n'ai aucune idée quel est le problème, puisque stringstream est censé convertir l'int en chaîne non signé.

Une aide? J'utilise C++ 98

+1

Mettez ce code : '(idPair-> first == myPairsList.begin())? "": "," 'dans des parenthèses. – sgarizvi

+0

"Méfiez-vous de l'opérateur?: Et mettez-le entre parenthèses" - la première règle ici http://cpphints.com/hints/1 :) –

Répondre

3
ss << ((idPair->first == myPairsList.begin()) ? "" : ",") << idPair->first; 
    ^            ^

add parenthessis

Sans eux, c'est ce qu'il essaie de faire:

ss << (idPair->first == myPairsList.begin()) ? "" : "," << idPair->first; 
                ~~~~^^~~~~~~~~~~~~~~ 

Vous pouvez voir le C++ Operator Precedence