我正在進行一項任務,我必須將程序拆分爲.cpp和.h文件模塊,並且出現奇怪的錯誤。在我的一個.cpp文件中,我有代碼'string'/'cin'未在此範圍內聲明
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
bool getYesNoResponse()
{
string response;
getline (cin, response);
while (response.size() == 0 ||
(response[0] != 'y' && response[0] != 'Y'
&& response[0] != 'n' && response[0] != 'N'))
{
if (response.size() > 0)
cout << "Please respond 'yes' or 'no'. " << flush;
getline (cin, response);
}
return response[0] == 'y' || response[0] == 'Y';
}
我收到錯誤error: 'string' was not declared in this scope
。我不應該編輯給我的實際代碼(我們只應該編寫包含並在.h文件中定義函數),但是我想查看字符串問題是否是一次性的,所以我在「string response;
」行中將string
替換爲std::string
,並且問題停止;除了我接着在下一行獲得error: 'cin' was not declared in this scope
。我有我的#include
<string>
,<iostream>
和<fstream>
,所以我很困惑,爲什麼它不工作。任何意見,如何解決這個問題,而不改變我的源代碼,將不勝感激!
ü可以嘗試使用'命名空間std'的包括 –
剛過@SakthiKumar「沒有改變我的源代碼」 – hvd
你已經找到了解決問題的正確方法,但是你拒絕那個解決方案。事實上,你拒絕任何*解決方案在你的問題。你接着要求什麼? – hvd