No match for != operator in Code::Blocks

Started by
8 comments, last by jniblick 12 years, 3 months ago
Can someone who knows C++ better than I do please read through my code and tell me what I'm doing to keep getting this error.


#include <iostream>
using namespace std;
string nameCharacter(string name);
int main()
{
string charName;
cout << "Hello world!" << endl;
cout << "Please enter your character's name..." << endl;
cin >> charName;

return 0;
}
string nameCharacter(string blankName) {
string charName;
charName = blankName;
string endCharName = NULL;
if (charName != NULL)
{
endCharName = charName;
return charName;
}
else
{
cout << "Error" << endl;
}
}
#include <iostream>
using namespace std;
string nameCharacter(string name);
int main()
{
string charName;
cout << "Hello world!" << endl;
cout << "Please enter your character's name..." << endl;
cin >> charName;

return 0;
}
string nameCharacter(string blankName) {
string charName;
charName = blankName;
string endCharName = NULL;
if (charName != NULL)
{
endCharName = charName;
return charName;
}
else
{
cout << "Error" << endl;
}
}
Advertisement
EDIT: I made a mistake in what I said. No more C++ comments for me :D

Yo dawg, don't even trip.

If you want to use all the bells and whistles of std::string, you should #include <string>.

In C++, if you want to use operators on objects (+, -, ==, !=), the objects have to define them as part of their class. std::string does not define a != operator. It does have a compare() and equals() functions so use those instead.

Operators must be defined, but need not be defined as member functions ("part of the class"). <string> defines the free function operator!= overload, so != can be used with string objects.

The problem with the OPs code is that <string> is not included, but std::string is forward-declared (probably via <iostream>).
I tried including the string header, and got the same error message.
I missed the fact that you were comparing your strings against NULL. NULL isn't a string. If you want to see if a string is empty or not, use the empty() member function.
I don't think std::string and NULL are compatible. Instead, just define the std::string, and check if .empty() is true or not:



string nameCharacter(string blankName) {
string charName;
charName = blankName;
string endCharName;

if (!charName.empty())
{
endCharName = charName;
return charName;
}
else
{
cout << "Error" << endl;
}
}


EDIT: SiCrane beat me by a minute.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

You cant compare string to NULL. This only works for string pointers.

EDIT: Triple kill.. haha

I tried including the string header, and got the same error message.


you try to compare charName with NULL (Which is normally defined as 0) , since charName is not a pointer the comparison makes no sense, (comparison on std:strings is only defined for string vs string, not string vs int)

Edit: Beaten badly by multiple people.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Thank you all so much for the help.

This topic is closed to new replies.

Advertisement