String command error - C2679
I'm currently teaching myself C++ from the book, "Learn C++ by Making Games." I'm still at the beginning of the book and have been doing all the examples as is says however; I've come across an example dealing with String Variables. I've already read about constants, variables, and literals. Keep in mind, I am using Visual C++ 2008 so I'm still learning the kinks with that as well. Here is my conflict that I am trying to understand.
Also, since I'm still early in the book, it says to treat strings like any other variable, but make sure I declare usage of the standard namespace, as in Example 2.1 shown in Listing 2.12, or the compiler may not recognize the string type. -- I think I know what this part is talking about.
This was the example from the book.
/******************************
Learn C++ by Making Games
Example 2.1
******************************/
#include <iostream>
using namespace std;
int main()
{
const string myString = "This is chapter 2.";
cout << myString << endl;
return 0;
}
When I clicked on "build," I recieved the error...error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion)
The book tells me nothing after this regarding the example.
After messing around and looking on the net for something, I inserted #include <string> and that made it work but I have no idea why. Why didn't it work without it. Please help a newbie... Thanks!
I'm currently teaching myself C++ from the book, "Learn C++ by Making Games." I'm still at the beginning of the book and have been doing all the examples as is says however; I've come across an example dealing with String Variables. I've already read about constants, variables, and literals. Keep in mind, I am using Visual C++ 2008 so I'm still learning the kinks with that as well. Here is my conflict that I am trying to understand.
Also, since I'm still early in the book, it says to treat strings like any other variable, but make sure I declare usage of the standard namespace, as in Example 2.1 shown in Listing 2.12, or the compiler may not recognize the string type. -- I think I know what this part is talking about.
This was the example from the book.
/******************************
Learn C++ by Making Games
Example 2.1
******************************/
#include <iostream>
using namespace std;
int main()
{
const string myString = "This is chapter 2.";
cout << myString << endl;
return 0;
}
When I clicked on "build," I recieved the error...error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion)
The book tells me nothing after this regarding the example.
After messing around and looking on the net for something, I inserted #include <string> and that made it work but I have no idea why. Why didn't it work without it. Please help a newbie... Thanks!
Last edited: