使用 MinGW 的 c++ unicode 程序中的错误

error in c++ unicode program using MinGW

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

int main()
{
    wstring str = L"こんにちは";
    wcout<<str<<endl;
    system("pause");
}

我正在尝试从 C++ 程序打印日语 (hello),但出现错误。我已经使用 UNICODE 编码将这个程序保存在记事本中,然后使用 MinGW 4.7.2 编译它 以下错误:

E:\GCC test>g++ -c unicode.cpp
unicode.cpp:1:1: error: stray '7' in program
unicode.cpp:1:1: error: stray '6' in program
unicode.cpp:1:1: error: stray '#' in program
unicode.cpp:3:4: error: invalid preprocessing directive #i
unicode.cpp:5:4: error: invalid preprocessing directive #i
unicode.cpp:1:5: error: 'i' does not name a type
unicode.cpp:11:2: error: 'i' does not name a type

根据错误,您似乎有一个 UTF-16LE 文件,带有 BOM (Byte Order Mark),而且编译器不喜欢那样。

76 = 0xfffe = UTF-16LE BOM

尝试删除 BOM,and/or尝试不同的编码。 UTF-8 是一种出色的编码,不需要 BOM,大多数编译器和许多其他工具都能理解。


至于Unicode,不是二进制字符编码。然而,有一些编码与 Unicode“绑定”。 UTF-8 和 UTF-16 可能是最常见的此类编码。

如果编辑器提供以“Unicode 编码”保存文件,请尽量远离该编辑器。如果那个编辑器是记事本,那么就有更多理由远离它。给自己找一个合适的编程编辑器,一个理解编码和 EOL,并且有语法高亮等的编辑器。