• Advertisement
Sign in to follow this  

about std::string

This topic is 4752 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

I have two little questions about std::string:
#include <iostream>

void func(const std::string& s)
{
	//std::cout << s << std::endl;  /* Q1 */
	std::cout << s.c_str() << std::endl;
}

void func(const char *s)
{
	std::cout << s << std::endl;
}

int main(int argc, char* argv[])
{
	func("hello");  /* Q2 */
	return 0;
}

Q1. why this error message ? a.cpp(9) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion Q2. func("hello") calls to func(char *) if there are the two version, one string and other char*, why? and how to override this ? thanks

Share this post


Link to post
Share on other sites
Advertisement
A1: You forgot to #include <string>
A2: "hello" is a char const *, if you want func(std::string const &) to be called instead, use func( std::string( "hello" ) ).

Share this post


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

  • Advertisement