about std::string

Started by
1 comment, last by Silly_con 19 years, 2 months ago
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
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" ) ).
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
how silly I am! :) thnaks for reply

This topic is closed to new replies.

Advertisement