[英]How to find adjust code to compile with cls and PAUSE in the C++
我不知道使用g ++進行錯誤編譯的原因。 我正在使用過時的類嗎?
我的MWE:
#include <iostream>
using namespace std;
int main()
{
system("cls");
cout << "\n\n";
cout <<"\n\t\xDC\xDC\xDB\xDB\xDB\xDB\xDC\xDC";
cout <<"\n\t\xDF0\xDF\xDF\xDF\xDF0\xDF";
cout <<"\n\n";
cout <<"\n\t\xDC\xDC\xDB\xDB\xDB\xDB\xDB\xDB\xDB";
cout <<"\n\t\xDF0\xDF\xDF\xDF\xDF\xDF00\xDF";
cout << "\n\n";
cout << endl;
system("PAUSE");
return 0;
}
**
編譯結果:
$ g++ 14.cpp
14.cpp: In function ‘int main()’:
14.cpp:11:9: warning: hex escape sequence out of range
11 | cout <<"\n\t\xDF0\xDF\xDF\xDF\xDF0\xDF";
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14.cpp:11:9: warning: hex escape sequence out of range
14.cpp:16:9: warning: hex escape sequence out of range
16 | cout <<"\n\t\xDF0\xDF\xDF\xDF\xDF\xDF00\xDF";
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14.cpp:16:9: warning: hex escape sequence out of range
$ ./a.out
sh: cls: command not found
十六進制轉義序列應該被格式化為 \\Xnn
。 它抱怨是因為您提供了兩個以上的十六進制值,如\\xDF0
和\\xDF00
。
- 編輯 -
為了完整起見 ,引用cppreference :
十六進制轉義序列沒有長度限制,並且終止於不是有效十六進制數字的第一個字符。 如果由單個十六進制轉義序列表示的值不適合此字符串文字(char,char16_t,char32_t或wchar_t)中使用的字符類型表示的值的范圍,則結果不確定。
為了停止轉義十六進制字符,請使用cppreference的另一個引號:
如果有效的十六進制數字在字符串文字中跟隨十六進制轉義符,則它將無法編譯為無效的轉義序列。 字符串串聯可以用作解決方法:
//const char* p = "\xfff"; // error: hex escape sequence out of range
const char* p = "\xff""f"; // OK: the literal is const char[3] holding {'\xff','f','\0'}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.