Jump to content
  • Advertisement
Sign in to follow this  
jniblick

No match for != operator in Code::Blocks

This topic is 2520 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
}
}

Share this post


Link to post
Share on other sites
Advertisement

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>).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!