无法从const char []转换为std:string*

0 c++ c++-cli visual-c++

在visual c ++ cli项目文件中,我创建了以下类(c ++类型).无法解析适合名称变量的字符串或char类型.

#include <vector>
#include <string.h>
using namespace std ;

class MyClass 
{
public :
int x;
int y;
string * name;

void foo() { name = "S.O.S" ;}
};
Run Code Online (Sandbox Code Playgroud)

Ps.型铸造错误

hmj*_*mjd 6

您需要进行以下更改:

#include <string> // not <string.h>

class MyClass
{
public:
    int x;
    int y;
    string name; // not string*
};
Run Code Online (Sandbox Code Playgroud)

编辑:

为了解决eliz的评论,一个小例子:

#include <iostream>
#include <string>

using namespace std;

class MyClass
{
public:
    int x;
    int y;
    string name;

    string foo()
    {
        name = "OK";
        return name;
    }
};

int main()
{
    MyClass m;

    // Will print "OK" to standard output.
    std::cout << "m.foo()=" << m.foo() << "\n";

    // Will print "1" to standard output as strings match.
    std::cout << ("OK" == m.foo()) << "\n";

    return 0;
}
Run Code Online (Sandbox Code Playgroud)