【发布时间】:2018-04-16 20:14:34
【问题描述】:
我有以下 C++ 代码:
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
#include <cstring>
using namespace std;
int main() {
istringstream inSS;
string title;
string col1;
string col2;
string val;
int numCommas;
vector<string> stringData();
vector<int> intData();
cout << "Enter a title for the data:" << endl;
getline(cin, title);
cout << "You entered: " << title << endl << endl;
cout << "Enter the column 1 header:" << endl;
getline(cin, col1);
cout << "You entered: " << col1 << endl << endl;
cout << "Enter the column 2 header:" << endl;
getline(cin, col2);
cout << "You entered: " << col2 << endl << endl;
while (1) {
cout << "Enter a data point (-1 to stop input):" << endl;
getline(cin, val);
if(strcmp(val.c_str(), "-1") == 0) {
break;
}
inSS >> stringData >> intData;
cout << "Data string: " << stringData << endl;
cout << "Data integer: " << intData << endl;
}
return 0;
}
有问题的错误:
main.cpp: In function 'int main()': main.cpp:46:9: error: no match for 'operator>>' (operand types are 'std::istringstream {aka std::cxx11::basic_istringstream<char>}' and 'std::vector<std::cxx11::basic_string<char> >()')
inSS >> stringData >> intData;
~~~^~~~~~~~~~~
这个错误是什么意思?我该如何解决?
【问题讨论】:
-
stringData是一个向量,向量不会为>>提供重载。你希望inSS >> stringData >> intData;做什么? -
这很简单。意思是您正在使用
basic_istringstream(即inSS)和vector(即stringData和intData),其中没有operator>>运算符(您在@987654332 中使用@)。 -
那么我如何将用户输入存储到向量中?对于这个作业,我们应该使用 istringstream 和向量。
标签: c++ iostream most-vexing-parse