C ++ stringstream错误:不匹配运算符<<对于操作数类型std :: string和const unsigned int

时间:2016-01-11 08:52:47

标签: c++ stringstream

我有这个函数接收const vector<unsigned int, 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();
}

在使用// ERROR选择的行中,我收到以下错误:

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

我不知道问题是什么,因为stringstream应该将unsigned int转换为string。

有任何帮助吗?我使用的是C ++ 98

1 个答案:

答案 0 :(得分:4)

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

添加父母

没有它们,这就是它试图做的事情:

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

您可以看到C++ Operator Precedence