Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;从字符串到常量字符没有可行的转换* 我使用C++(使用CERN的根框架),我对字符串有一个小问题。我试图使用用户在代码前面定义的字符串来标记直方图轴。以下是守则的相关部分: string xlabel; ... cout << "Enter x-axis label:" << endl; getline(cin >> ws, xlabel); ... hist->GetXaxis()->SetTitle(xlabel); 字符串xlabel; ... cout-ws,xlabel); ... hist->GetXaxis()->SetTitle(xlabel);_C++_String_Char - Fatal编程技术网 GetXaxis()->SetTitle(xlabel);,c++,string,char,C++,String,Char" /> GetXaxis()->SetTitle(xlabel);,c++,string,char,C++,String,Char" />

C++;从字符串到常量字符没有可行的转换* 我使用C++(使用CERN的根框架),我对字符串有一个小问题。我试图使用用户在代码前面定义的字符串来标记直方图轴。以下是守则的相关部分: string xlabel; ... cout << "Enter x-axis label:" << endl; getline(cin >> ws, xlabel); ... hist->GetXaxis()->SetTitle(xlabel); 字符串xlabel; ... cout-ws,xlabel); ... hist->GetXaxis()->SetTitle(xlabel);

C++;从字符串到常量字符没有可行的转换* 我使用C++(使用CERN的根框架),我对字符串有一个小问题。我试图使用用户在代码前面定义的字符串来标记直方图轴。以下是守则的相关部分: string xlabel; ... cout << "Enter x-axis label:" << endl; getline(cin >> ws, xlabel); ... hist->GetXaxis()->SetTitle(xlabel); 字符串xlabel; ... cout-ws,xlabel); ... hist->GetXaxis()->SetTitle(xlabel);,c++,string,char,C++,String,Char,其中最后一行只是ROOT使用的语法(这里的xlabel通常用引号表示,您可以输入您想要的标签,但我正在尝试输入代码前面定义的字符串。) 无论如何,当我编译此文件时,会出现以下错误: error: no viable conversion from 'string' (aka 'basic_string<char>') to 'const char *' hist->GetXaxis()->SetTitle(xlabel);

其中最后一行只是ROOT使用的语法(这里的xlabel通常用引号表示,您可以输入您想要的标签,但我正在尝试输入代码前面定义的字符串。)

无论如何,当我编译此文件时,会出现以下错误:

error: no viable conversion from 'string'
      (aka 'basic_string<char>') to 'const char *'
        hist->GetXaxis()->SetTitle(xlabel);
                                   ^~~~~~
错误:从“字符串”没有可行的转换
(也称为“基本字符串”)到“常量字符*”
hist->GetXaxis()->SetTitle(xlabel);
^~~~~~
我曾尝试将xlabel重新定义为const char*但它也不喜欢这样。有人对我如何定义这个字符串有什么建议吗

提前谢谢

这样做:

hist->GetXaxis()->SetTitle(xlabel.c_str());
//                               ^^^^^^^^

你错过了对c_str()的呼叫。成功了!我只是查了一下c_str()的功能,现在完全可以理解了。非常感谢你!