首页 > 解决方案 > 如何用字符串初始化声明的 istringstream?

问题描述

在 C++ 中,如何用字符串初始化已声明的 istringstream?

例子.hpp

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

例子.cpp

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

错误

错误:不匹配调用 '(std::istringstream {aka std::basic_istringstream}) (const char [8])'</p>

标签: c++getlineistringstream

解决方案


要构造一个类成员,您需要使用类成员初始化列表。一旦你进入构造函数的主体,所有的类成员都已经被构造出来,你所能做的就是分配给它们。要使用成员初始化列表,您需要将构造函数更改为

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

推荐阅读