C++ 使用带有变量作为参数的自定义 ifstream 类时出错
C++ Error using a custom ifstream class with a variable as argument
我正在使用自定义的ifstream类
class plb_ifstream : public Parallel_istream {
public:
plb_ifstream();
explicit plb_ifstream(const char* filename,
std::istream::openmode mode = std::ostream::in );
~plb_ifstream();
virtual std::istream& getOriginalStream();
bool is_open();
void open(const char* filename, std::istream::openmode mode = std::ostream::in);
void close();
bool good();
private:
plb_ifstream(plb_ifstream const& rhs);
plb_ifstream& operator=(plb_ifstream const& rhs);
private:
DevNullBuffer devNullBuffer;
std::istream devNullStream;
std::ifstream *original;
};
这适用于单个文件,例如
plb_ifstream ifile("geometry.dat");
但是,当我尝试在参数中使用变量(在 for 循环中)时,例如
for(plint num=1; num<4; num++)
{
std::ostringstream ostr;
ostr <<num<<".dat";
std::string var = ostr.str();
pcout <<"Reading geometry.."<<endl;
plb_ifstream ifile(ostr.str());
ifile >> boolMask;
pcout<<"done..."<<endl;}
我收到以下错误
error: no matching function for call to ‘plb::plb_ifstream::plb_ifstream(std::basic_ostringstream<char>::__string_type)’|
note: candidates are:|
note: plb::plb_ifstream::plb_ifstream(const plb::plb_ifstream&)|
note: no known conversion for argument 1 from ‘std::basic_ostringstream<char>::__string_type {aka std::basic_string<char>}’ to ‘const plb::plb_ifstream&’|
note: plb::plb_ifstream::plb_ifstream(const char*, std::ios_base::openmode)|
note: no known conversion for argument 1 from ‘std::basic_ostringstream<char>::__string_type {aka std::basic_string<char>}’ to ‘const char*’|
note: plb::plb_ifstream::plb_ifstream()|
note: candidate expects 0 arguments, 1 provided|
我确实找到了其他一些解决方案,但它们没有利用ifstream。如果有解决方法仅使用 ifstream 它们,我将不胜感激
您可能需要替换:
plb_ifstream ifile(ostr.str());
跟
plb_ifstream ifile(ostr.str().c_str());
获取 C 字符串等效项
你还没有编写一个plb_ifstream
构造函数来接受一个std::string
,例如你的std::ostringstream
的.str()
返回的。 追加进一步.c_str()
或添加构造函数。
相关文章:
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- C++自定义比较函数
- 如何比较自定义类的std::变体
- std::设置自定义比较器
- 如何正确实现和访问运算符的各种自定义枚举器
- flutter:即使shouldRepaint()返回true,自定义画家也不会重新绘制
- 自定义先决条件对移动分配运算符有效吗
- 使用VS Code和CMake Tools运行自定义命令
- 如何创建从Maya(或类似程序)到虚幻引擎的自定义数据导出插件
- std::ranges::elements_view,用于自定义类似元组的数据
- 跟随整数索引列表的自定义类迭代器
- 参数化自定义CMake工具链
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- 如何在自定义类中启用'auto loops'?
- 使用QJsEngine在Qt中注册自定义类型
- Qt自定义QPush按钮未显示在布局上
- 自定义对象的dlib序列化在gcc中失败
- 自定义类对象的C++向量 - 复制构造函数已删除 - std::ifstream
- C++ 使用带有变量作为参数的自定义 ifstream 类时出错