这个string转换成char*为何不成功
这个string转换成char*为什么不成功?
错误提示为
error C2065: 'str' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\111\111.cpp(12) : error C2228: left of '.c_str' must have class/struct/union type
执行 cl.exe 时出错.
但是我在网上看到的写法都差不多是这样的,不知怎么就没有定义str?
------解决方案--------------------
char *p = name.c_str();
------解决方案--------------------
const char *p = name.c_str();
------解决方案--------------------
------解决方案--------------------
1. 变量 str 从哪来的?
2. c_str 是 const 的
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name = "abcyuiioort";
char *p = str.c_str();
return 0;
}
错误提示为
error C2065: 'str' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\111\111.cpp(12) : error C2228: left of '.c_str' must have class/struct/union type
执行 cl.exe 时出错.
但是我在网上看到的写法都差不多是这样的,不知怎么就没有定义str?
------解决方案--------------------
char *p = name.c_str();
------解决方案--------------------
const char *p = name.c_str();
------解决方案--------------------
//char *p = str.c_str();
char const *p = name.c_str();
------解决方案--------------------
1. 变量 str 从哪来的?
2. c_str 是 const 的