【发布时间】:2017-10-11 22:39:22
【问题描述】:
我在堆栈溢出时看到了这段代码,
#include <sstream>
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<string> strings;
istringstream f("denmark sweden india us");
string s;
while (getline(f, s, ' ')) {
cout << s << endl;
strings.push_back(s);
}
}
但我似乎无法理解为什么它在 main 方法之外不起作用。我有两个文件,一个有 main 方法,另一个我想在其中实现此代码。
这是我尝试过的
文件1.h
#include <iostream>
#include <iomanip>
#include <string>
#include <string>
#include <sstream>
#include <vector>
#include <iterator>
using pep::vector;
using std::cout;
using std::endl;
using std::string;
double evaluate(string str)
{
vector<string> strings;
istringstream f(str);
string s;
while (getline(f, s, ' '))
{
out << s << endl;
strings.push_back(s);
}
return 0;
}
文件2.cpp
#include "file1.h"
int main()
{
double answer = evaluate("3.0 4.0 +");
}
我收到了这些错误:
file1.h: In function ‘double evaluate(std::__cxx11::string)’:
file1.h:89:5: error: ‘istringstream’ was not declared in this scope
istringstream f(str);
^~~~~~~~~~~~~
file1.h:89:5: note: suggested alternative:
In file included from /usr/include/c++/6/ios:38:0,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from Stack.h:5:
/usr/include/c++/6/iosfwd:150:38: note: ‘std::istringstream’
typedef basic_istringstream<char> istringstream;
^~~~~~~~~~~~~
file1.h:91:20: error: ‘f’ was not declared in this scope
while (getline(f, s, ' '))
^
file1.h:93:5: error: ‘out’ was not declared in this scope
out << s << endl;
任何帮助将不胜感激
【问题讨论】:
-
还尽可能详细地描述“不工作”(错误消息、预期/收到的输出/...)。这句话本身并不意味着任何有用的东西
-
@FrançoisAndrieux 我编辑了它。如果还需要什么,请告诉我。谢谢
-
@UnholySheep 问题是它无法编译。
-
编译器错误通常是非常描述性的,告诉你哪里出了问题(我不知道你为什么没有在你的问题中包含错误)
-
istringstream f(str);->std::istringstream f(str);(或者像其他人一样在顶部放置using std::istringstream)和out->cout。基本上正是错误告诉你的内容
标签: c++