我正在进行一项任务,我必须将程序拆分为.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