0

私は.C++で文字列を分割しようとしています。次に、分割された最初の文字列を受け入れる別のメソッドに渡す必要がありますconst char* key..しかし、そうするたびに、常に例外が発生します-

以下は私のコードです -

istringstream iss(key);
std::vector<std::string> tokens;
std::string token;
while (std::getline(iss, token, '.')) {
    if (!token.empty()) {
        tokens.push_back(token);
    }
}

cout<<"First Splitted String: " <<tokens[0] << endl;
attr_map.upsert(tokens[0]); //this throws an exception
}

以下は、AttributeMap.hh ファイルの upsert メソッドです。

bool upsert(const char* key);

そして、以下は私がいつも得る例外です-

no matching function for call to AttributeMap::upsert(std::basic_string<char>&)

不足しているものはありますか?

4

2 に答える 2

0

string::c_strを使用する必要があります

attr_map.upsert(tokens[0].c_str())
                        //^^^

機能の詳細についてはリファレンスを確認できc_str()ます。

upsert関数が を期待しているため、エラーが発生していますが、型の不一致const char*を渡しています。std::string

于 2013-10-16T20:11:46.193 に答える