我需要将字符串传递给仅接受 char * 的套接字 send() 函数。所以在这里我试图转换它:
void myFunc(std::string str) //Taking string here const is good idea? I saw it on some examples on web
{
char *buf = str.c_str; //taking buf const is good idea?
std::cout << str;
}
int main()
{
const std::string str = "hello world";
myFunc(str);
return 0;
}
给出错误:
test.cpp:6:18: error: cannot convert ‘std::basic_string<_CharT, _Traits, _Alloc>::c_str<char, std::char_traits<char>, std::allocator<char> >’ from type ‘const char* (std::basic_string<char>::)()const’ to type ‘char*’