c++istringstream

Why am i getting error when i try to assign string to istringstream object in visual studio in c++?


The following class takes a string line as input from the user. My aim is to store it in the istringstream iss and then to extract various data from the istringstream iss into various variables. But, when I try to initialize iss with string user_input, I am getting this error when I compile the file:

error C2064: term does not evaluate to a function taking 1 arguments

I think the problem is in the line iss{user_input}; Please explain why I am getting this error.

_input.h header file

#pragma once

#include <string>
#include<sstream>
#include "_date.h"
#include "_time.h"

using namespace std;

//definition of class input
class _input
{
    string user_input;
    istringstream iss;

    //datamembers to be sent
    int esno,eday, emonth, eihours, eimins, eohours, eomins, emo;
    char eiap, eoap;
    string enotes;

public:
    _input();

    void get_input();
    void process_data();
};

_input.cpp file

#include "_input.h"

_input::_input() : user_input { "Nothing Entered" }, iss{ "" }, esno{ 0 }, eday{ 0 }, emonth{ 0 },
      eihours{ 0 }, eimins{ 0 }, eohours{ 0 }, eomins{ 0 }, emo{ 0 }, eiap{ 'n' }, eoap{ 'n' }, enotes{ "" }
{

}

void _input::get_input() // to store in iss
{
    cout << "Enter the entry : Format (Date - In Time - Out Time - Money Owed - Notes)" << endl;
    getline(cin, user_input);
    iss{user_input};  //THIS IS WHERE I GET THE ERROR
}

void _input::process_data()
{
    iss >> eday >> emonth >> eihours >> eimins >> eiap >> eohours >> eomins >> eoap >> emo >> enotes;
    ++esno;
    
}

Solution

  • The short version is you cannot initialize an object twice. iss is declared and constructed as a private member of your class. It already exists when you attempt iss{user_input} -- that's not allowed. You don't want Initialization, instead you simply want to replace the content of iss with what is in user_input. For that you use std::basic_istringstream::str. The member function str allows you to get or set the contents of your istringstream.

    A short example:

    #include <iostream>
    #include <sstream>
    #include <string>
    
    int main (void) {
        
        std::istringstream iss {};                      /* iss is declared/constructed */
        std::string s {};
        
        std::cout << "enter s: ";
        if (!getline(std::cin, s))
            return 1;
        
        iss.str(s);                                     /* replace iss content with s */
        std::cout << "iss has: "<< iss.str() << '\n';   /* check iss contains s */
        while (iss >> s)                                /* read each string in iss */
            std::cout << s << '\n';                     /* output string */
    }
    

    Example Use/Output

    $ ./bin/iss
    enter s: my dog has fleas
    iss has: my dog has fleas
    my
    dog
    has
    fleas
    

    Additional tip, have a look at Why is “using namespace std;” considered bad practice?.

    Look things over and let me know if you have further questions.