0
我有以下的C++代碼:爲什麼我在嘗試從istringstream中讀取數據時出錯?
#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;'做什麼? – Carcigenicate
這相當簡單。它是說你正在使用沒有'operator >>'操作符的'basic_istringstream'(即'inSS')和'vector'(即'stringData'和'intData') 'inSS >> stringData >> intData;')。 –
那麼我該如何將用戶輸入存儲到矢量中?對於這個任務,我們應該使用istringstream和向量。 – Chase