0

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?

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
Chase
  • 93
  • 1
  • 1
  • 7
  • `stringData` is a vector, and vectors don't supply an overload for `>>`. What do you expect `inSS >> stringData >> intData;` to do? – Carcigenicate Nov 04 '17 at 02:57
  • It's fairly straightforward. It's saying you're using a `basic_istringstream` (i.e., `inSS`) and a `vector` (i.e., `stringData` and `intData`) for which there is no `operator>>` operator (which you are using in `inSS >> stringData >> intData;`). – James Adkison Nov 04 '17 at 02:58
  • Well then how do I store the user input into a vector? For this assignment we are supposed to use istringstream and vectors. – Chase Nov 04 '17 at 03:28

1 Answers1

0

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.
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065