【发布时间】:2020-08-10 06:44:09
【问题描述】:
我在 VS2019 中的代码不起作用。 在最后一行代码编译器抛出错误。
#include <iostream>
#include <string>
using namespace std;
struct struct1
{
string name;
};
void main()
{
struct1* obj1 = new struct1();
obj1->name = "Hello";
// compiler says 'initializing': cannot convert from 'const _Elem *' to 'char [25]'
char str[25] = (obj1->name).c_str();
}
【问题讨论】:
-
初始化数组是行不通的。
-
很难理解当您拥有
string时为什么要将其复制到数组中。正如您发现的那样,数组非常有限。