[英]Cannot convert from const char[] to std:string *
In a visual c++ cli project file i created the following class (c++ type) . 在可视的c ++ cli项目文件中,我创建了以下类(c ++类型)。 not able to declere a string or char type suitable for name variable.
无法解析适合名称变量的字符串或char类型。
#include <vector>
#include <string.h>
using namespace std ;
class MyClass
{
public :
int x;
int y;
string * name;
void foo() { name = "S.O.S" ;}
};
Ps . 附言 type casting err
打字错误
You need to make the following changes: 您需要进行以下更改:
#include <string> // not <string.h>
class MyClass
{
public:
int x;
int y;
string name; // not string*
};
EDIT: 编辑:
To address comments by eliz , a small example: 为了解决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;
}
If name
is of type string *
, then you must call one of the string constructors. 如果
name
的类型为string *
,则必须调用字符串构造函数之一。
name = new string("S.O.S");
And don't forget to release your string in the destructor (~MyClass())! 并且不要忘记在析构函数(〜MyClass())中释放您的字符串!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.