-1

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])’

Eugene
  • 10,957
  • 20
  • 69
  • 97

1 Answers1

2

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") {}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • Found it can also be done in the constructor with `this->_workingStream.str("exampletext");` – Eugene Nov 03 '18 at 22:59
  • 2
    @Eugene - generally speaking, its better to initialise class members in the initialiser list than the constructor body, if possible. Even if you don't, the object will need to be initialised before you can set it in the constructor body, so better to avoid a "initialise then reset" cycle. – Peter Nov 03 '18 at 23:52