Reputation: 3
Trying to resolve an error resulting from use of the std::count function. I've gone through the template in xutility and it appears that the types match, so I'm not sure why I'm getting these errors. I'm using the count function in two instances and they're throwing two different errors.
The first error is:
C2446 '==': no conversion from 'const char [2]' to 'int'
Here's the code that creates this error (it's the beginnings of a comparator for sorting):
struct vectorComparator
{
bool operator ()(const string& string1, const string& string2)
{
if (count(string1.begin(), string1.end(), "!") < count(string2.begin(), string2.end(), "!") + 1)
return string1 < string2;
return string1 < string2;
}
};
The second error is very similar:
C2446 '==': no conversion from 'const _Ty' to 'int'
Here's the code that creates this error:
string myClass::myFunction(string parentToken)
{
string messageChunk{};
vector<string> tokens;
for (auto it = messageMap.begin(); it != messageMap.end(); ++it)
if (it->first.find(parentToken) != string::npos &&
count(it->first.begin(), it->first.end(), elementDelimiter) == count(parentToken.begin(), parentToken.end(), elementDelimiter) + 1)
{
tokens = explode(elementDelimiter, it->first);
messageChunk += (messageChunk.size() > 0 ? "," : "") + '\"' + tokens[tokens.size()] + "\":" + buildValueString(it->second);
messageMap.erase(it);
}
messageChunk = '{' + messageChunk + '}';
if (arrayTokens.find(tokens[tokens.size() - 1]) != string::npos)
messageChunk += '\"' + tokens[tokens.size() - 1] + "\":[" + messageChunk + ']';
else messageChunk += '\"' + tokens[tokens.size() - 1] + "\":" + messageChunk;
return messageChunk;
}
With the understanding that I'm new to this and I'm sure there's plenty of ways to streamline the code (and I'll take all input), I'm primarily looking for help with resolving the two errors. It seems to be a type issue, but I can't resolve it. In the second sample elementDelimiter is defined as const char* and is equal to "!". Thanks for any help or guidance you can provide!
Upvotes: 0
Views: 221
Reputation: 1072
So: 1 case: string consist of char. And "count" tries to compare string ("!") with int-like-char. It can't. You can use '!' instead of "!" (single quotes define single char).
2 case: I think it has the same error (What is the messageMap type?).
In second case you can use 'count' with predicate:
count(it->first.begin(), it->first.end(), [elementDelimiter](char symbol){
// Here you should define your comparison operation. Like this:
return elementDelimiter[0] == symbol; })
Upvotes: 3