简体   繁体   English

错误:无法转换&#39;std :: string {aka std :: basic_string <char> 初始化时&#39;&#39;到&#39;char *&#39;

[英]error: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘char*’ in initialization

I'm tinkering with C++ (I have a COBOL, Perl and PHP background) and I'm having trouble splitting a string so that I can act on each parsed field. 我正在修补C ++(我有一个COBOL,Perl和PHP背景),我在分割字符串时遇到问题,这样我就可以对每个已解析的字段进行操作。 I believe the trouble is to do with my lack of understanding of how C++ handles variables. 我认为问题在于我对C ++如何处理变量缺乏了解。

I've slurped a file's contents into an array successfully and I can act on each complete line (eg search/replace text). 我已经成功地将文件的内容插入到数组中,并且我可以对每个完整的行进行操作(例如,搜索/替换文本)。 I also found some great examples of how to parse strings. 我还发现了一些如何解析字符串的很好的例子。 The problem occurs when I try to apply the parse examples to the strings in my array. 当我尝试将解析示例应用于我的数组中的字符串时,会发生此问题。

The strings are created via: 字符串通过以下方式创建:

    std::vector<std::string> File_Lines;
    ifstream file(filename.c_str());
    while ( file.good() ) {
      getline ( file, line, '\n' );
      string InputLine = string( line );
      File_Lines.push_back(line);
    }

One working example of parsing strings (that I found on this site) is: 解析字符串的一个工作示例(我在此站点上找到)是:

char myString[] = "The quick brown fox";
char *p = strtok(myString, " ");
while (p) {
  printf ("Token: %s\n", p);
  p = strtok(NULL, " ");
}

The problem starts when I try to feed my string to the code that does the parsing. 当我尝试将字符串提供给执行解析的代码时,问题就开始了。 Feeding it directly: 直接喂它:

char myString[] = File_Lines[array_counter];

gives "error: initializer fails to determine size of 'myString'" 给出“错误:初始化程序无法确定'myString'的大小”

If I try converting using "std::string" (as suggested in other answers on this site): 如果我尝试使用“std :: string”进行转换(如本网站其他答案所示):

std::string File_Line;
File_Line = File_Lines[array_counter];
char myString[] = File_Line;

...I get the same error. ...我犯了同样的错误。

Other answers suggested adjusting the code to: 其他答案建议将代码调整为:

char *myString = File_Line;

but that just gives me "error: cannot convert 'std::string {aka std::basic_string}' to 'char*' in initialization" 但这只是给了我“错误:无法在初始化时将'std :: string {aka std :: basic_string}'转换为'char *'”

I'm aware that the problem is due to my own ignorance, but I'd really appreciate any help on how to feed a string into a parser. 我知道问题是由于我自己的无知,但我真的很感激有关如何将字符串提供给解析器的任何帮助。

Also, if anyone has any simple explanation of how to convert between the data types, that would be great. 此外,如果任何人对如何在数据类型之间进行转换有任何简单的解释,那将是很好的。

This should be fine: 这应该没问题:

std::string ThisLine = File_Lines[array_counter]; // make a copy for strtok to mess up
char *p = strtok(&ThisLine[0], " "); // strtok needs a pointer to the first character
while (p) {
    printf ("Token: %s\n", p);
    p = strtok(NULL, " ");
}

But C++ has nicer ways to tokenize than strtok. 但是C ++比strtok有更好的标记方式。 For example, std::strstream with operator>> (for whitespace delimiters) or getline (for delimiter you specify). 例如, std::strstream with operator>> (用于空格分隔符)或getline (用于指定的分隔符)。

use the following: 使用以下内容:

const char * myString1 = File_Lines[array_counter].c_str();
char * myString=const_cast<char*>myString1;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:无法从&#39;std :: string * {aka std :: basic_string转换 <char> *}&#39;为&#39;std :: string {aka std :: basic_string <char> }&#39;| - error: could not convert from 'std::string* {aka std::basic_string<char>*}' to 'std::string {aka std::basic_string<char>}'| 错误无法转换 &#39;std::string {aka std::basic_string<char> }&#39; 到 &#39;char&#39; 赋值 - C++ - Error cannot convert 'std::string {aka std::basic_string<char>}' to 'char' in assignment - C++ 错误无法转换 'std::string {aka std::basic_string<char> }' 到 'int' 赋值</char> - Error cannot convert 'std::string {aka std::basic_string<char>}' to 'int' in assignment 错误无法转换 'std::string* {aka std::basic_string<char> *}' 到 'node*' 在分配</char> - Error cannot convert 'std::string* {aka std::basic_string<char>*}' to 'node*' in assignment 无法转换&#39;std :: string {aka std :: basic_string <char> }&#39;为&#39;char&#39;作为回报 - Cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘char’ in return 错误:无法转换 &#39;std::basic_string<char> &#39; 到 &#39;char&#39; 赋值 - error: cannot convert 'std::basic_string<char>' to 'char' in assignment 为什么我收到错误:无法转换 &#39;std::string {aka std::basic_string<char> }&#39; 到 &#39;char*&#39; 赋值? - Why am I getting error: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘char*’ in assignment? std :: string {aka std :: basic_string <char> 分配给&#39;&#39;&#39;char *&#39; - std::string {aka std::basic_string<char>}' to 'char*' in assignment| 错误:与“ operator *”不匹配(操作数类型为“ std::string {aka std basic_string <char> }&#39;和{aka std basic_string <char> }&#39;) - error: no match for 'operator*' (operand types are 'std: :string {aka std basic_string<char>}' and {aka std basic_string<char>}') 错误:无法转换&#39;std :: basic_string <char> 从&#39;到&#39;int&#39;分配 - error: cannot convert ‘std::basic_string<char>’ to ‘int’ in assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM