Search code examples
c++getlineistringstream

How to initialize declared istringstream with string?


In C++, how do you initialize a declared istringstream with a string?

example.hpp

#include <sstream>
class example{
    private:
        istringstream _workingStream;
    public:
        example();
}

example.cpp

example::example(){
    this->_workingStream("exampletext");
}

Error

error: no match for call to ‘(std::istringstream {aka std::basic_istringstream}) (const char [8])’


Solution

  • To construct a class member you need to use the class member initialization list. Once you are inside the body of the constructor all class members have all be constructed and all you can do is assign to them. To use the member initialization list you need to change the constructor to

    example::example() : _workingStream("exampletext") {}