[英]C++ endl with constants
我只是想讓自己熟悉從 Java 遷移而來的 C++ 的基礎知識。 我剛剛編寫了這個功能禁欲程序,遇到了一個錯誤test.cpp:15: error: expected primary-expression before '<<' token
我不知道為什么。
有人願意解釋為什么endl
不能使用常量嗎? 代碼如下。
//Includes to provide functionality.
#include <iostream>
//Uses the standard namespace.
using namespace std;
//Define constants.
#define STRING "C++ is working on this machine usig the GCC/G++ compiler";
//Main function.
int main()
{
string enteredString;
cout << STRING << endl;
cout << "Please enter a String:" << endl;
cin >> enteredString;
cout << "Your String was:" << endl;
cout << enteredString << endl;
return(0);
}
您的#define
末尾有一個分號。 這成為宏的一部分,因此預處理代碼如下所示:
cout << "C++ is working on this machine usig the GCC/G++ compiler"; << endl;
刪除分號,你應該沒問題。
PS:為此使用實常數而不是依賴預處理器通常是一個更好的主意:
const char *STRING = "C++ is working on this machine usig the GCC/G++ compiler";
你有一個;
在您的預處理器定義中。 請注意, #DEFINE STRING x
只是將整個 x 語句(包括;
)復制到引用它的位置。
此外,預處理器常量不是語言常量。 你應該使用const string STRING("C++ is working on this machine usig the GCC/G++ compiler");
您的#define
末尾有一個分號 - 這將被替換到您的代碼中,給出。
cout << "C++ is working on this machine usig the GCC/G++ compiler"; << endl;
因為您在 STRING 之后有一個分號。 刪除它並嘗試一下...
刪除;
在STRINGS
定義中
刪除; 從
#define STRING "C++ is working on this machine usig the GCC/G++ compiler"
刪除;
在#define STRING
末尾,然后重試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.