#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;
}
about std::string
I have two little questions about std::string:
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
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" ) ).
A2: "hello" is a char const *, if you want func(std::string const &) to be called instead, use func( std::string( "hello" ) ).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement