简体   繁体   中英

Why am I getting an error trying to read data from an istringstream?

I have the following C++ code:

#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;
}

Error in question:

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;
  ~~~^~~~~~~~~~~

What does this error mean? How do I fix it?

The error has to do with a combination of factors. For starters, let's look at this line:

inSS >> stringData >> intData;

Here, you're trying to read from an istringstream into a vector<string> and vector<int> . However, you can't use the stream extraction operator to read a vector from a stream - there's no fundamental reason why not, it's just that the standard doesn't allow it. You'll need to read that data one element at a time, which will likely require you to rewrite a lot of this code.

There's another more subtle issue here. These lines don't do what you think they do:

vector<string> stringData();
vector<int> intData();

These lines look like they declare variables named stringData and intData of type vector<string> and vector<int> , using the default constructors. Unfortunately, C++ interprets these, believe it or not, as function prototypes. That first one is a prototype of a function named stringData that takes no arguments (hence the emptiness between the parentheses) and returns a vector<string> , for example. To fix this, drop the parentheses. Just write

vector<string> stringData;
vector<int> intData;

To summarize:

  • You'll need to fundamentally make some changes to your code, since you can't read from an istringstream into a vector . That necessitates a logic update.
  • You'll need to fix the two declarations from earlier on by dropping the parentheses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM