声明变量时编译器错误 - 八进制序列 200 254 342

Compiler error when declaring a variable - octal sequence 200 254 342

本文关键字:八进制 变量 编译器 错误 声明      更新时间:2023-10-16

当此行不在注释中时:

double random_seed, participation_fee, ticket_revenue;

编译器生成以下错误:

main.cpp:24:2: error: stray ‘200’ in program
main.cpp:24:2: error: stray ‘254’ in program
main.cpp:24:2: error: stray ‘342’ in program
main.cpp:24:2: error: stray ‘200’ in program
main.cpp:24:2: error: stray ‘254’ in program

我已经尝试重新输入此行。我正在使用崇高文本作为文本编辑器。如何解决此问题?

这是整个函数:

void starting_game(vector<int>&players, vector<Player*> player_obj)
{
    int id, x, y, number=0;
    char pos;
    double random_seed,participation_fee,ticket_revenue;‬‬
    string input;
    cin >> number;
    for(int i = 0; i < number; i++)
    {
        cin >> id;
        cin.ignore(4,' ');
        cin >> x;
        cin.ignore(2,':');
        cin >> y;
        cin.ignore(2,':');
        cin >> pos;
        players.push_back(find_put(id, player_obj, x, y, pos));
    }
    //cin>>‫‪random_seed‬‬;//>>‫‪participation_fee‬‬>>‫‪ticket_revenue;‬‬
}

代码中有不可见的字符,这会阻止编译器正常工作,因为它无法处理它们。

在您的特定情况下,其中一个字符是 U+202c,使用 UTF-8 编码。它被称为"POP定向格式",并且不可见。

由于是隐形的,解决这个问题将很困难。甚至问题中的代码也包含该字符。

要修复它,您可以执行以下操作:

  • 尝试删除整行以及下一行,然后重新键入文本。在您的特定情况下,字符徘徊在行尾,如果您只是擦除内容行并重新键入它,而不会同时杀死换行符,则可能会保留这些字符。(通过@PatrickTrentin)

  • 使用删除所有非 ASCII 字符的脚本。这很容易用python完成。将以下代码粘贴到名为 script.py 的文本文件中,并使用 python3 执行它。

    #!/usr/bin/python3
    import argparse
    import sys
    parser = argparse.ArgumentParser()
    parser.add_argument("infile", type=argparse.FileType("rb"))
    parser.add_argument("outfile", type=argparse.FileType("wb"))
    args = parser.parse_args()
    with args.infile as inf:
       intext = inf.read().decode("utf-8")
       with args.outfile as outf:
          outf.write("".join(
              c for c in intext
              if ord(c) <= 127
          ).encode("utf-8"))
    

    用法python3 script.py input output不要两次输入相同的名称,它不起作用,你最终会得到一个空文件。无论如何,请在尝试此操作之前备份您的文件!

  • 使用十六进制编辑器手动删除所有非 ASCII 字符。不幸的是,我不知道任何易于使用的人。

在这种情况下,删除字符而不替换是正确的做法。在其他情况下(例如建议的副本),用更合适的字符替换有问题的字符更正确。这里的情况并非如此。