cout does not execute

Pages: 12
[quote]Gives me an error at the first "<<": "Error: no operator ("<<") matches these operands.


Call c_str( ) from agePrompt.
[/quote]

Like in
cout << c_str(agePrompt) << endl;?7
That gives me an error: Identifier "c_str" not known

What does c_str do, by the way?
c_str is a member function of the std::string class. It returns a c-style string (char array).

Try cout <<agePrompt.c_str() << endl;

Although cout << agePrompt << endl; should work fine.
Last edited on
Post the complete error message. ¿what operands?

agePrompt.c_str() http://www.cplusplus.com/reference/string/string/c_str/ You shouldn't need it.
With c_str(), it works. Without:

At the "<<" in cout << agePrompt: "Error: No operator "<<" matches these operands."
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
 
 
int main()
{
std::string agePrompt = "Enter your age!";
std::cout << agePrompt << std::endl;
return 0;
}


This works fine. What are you doing differently?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {

	cout << "Hello World\n+";

	string agePrompt = "Enter your age!";

	cout << agePrompt << endl;
	}

	return 0;
}


I need <string>?
Last edited on
If your compiler happily knows what a std::string is without #include <string> and then doesn't know how to apply the << operator to one, it's time to get a new compiler.
Last edited on
If you want to use string, you should include its header. Period.
Thank you, the header indeed fixed the problem. As I said, I just started learning, so all this stuff is new to me. Thanks anyways.
I am confused about why the addition is necessary to make the program work since I am using precisely the same set up:

Win 7 32bit
MSYS + MINGW
NetBeans

Having cut and paste the program from the first example it compiles and runs without a hitch.

My own guess is that you don't have the compilers set up properly. I'm quite happy to take a look at this if you'd like to take a look at how you're set up.


Meanwhile, I wouldn't be too fast to knock NetBeans. I know we all have our favourites but since using it I am reluctant to change. I have tried A LOT of IDEs.
Which other headers a given header includes is not defined by the standard. For example, a valid implementation could have an <iostream> that includes ALL the other headers. A user of such an implementation wouldn't need to include any other headers, but that doesn't make the code standard-compliant, as it assumes that the implementation follows a specific behavior.
In other words, if you want to use std::string and you want portability, you must include <string>. Simple as that.
Topic archived. No new replies allowed.
Pages: 12