#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;
}
}
No match for != operator in Code::Blocks
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.
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 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:
EDIT: SiCrane beat me by a minute.
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.
You cant compare string to NULL. This only works for string pointers.
EDIT: Triple kill.. haha
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement