没有合适的默认构造函数可用于初始化 ostream 指针
no appropriate default constructor available initialising ostream pointer
我正在尝试创建一个这样的日志记录类:
#include <iostream>
#include <fstream>
class logstream : public std::ostream
{
public:
logstream() : os(&std::cout), file_(false) {} // line 7
logstream(const char* filename) : os(new std::ofstream("file.txt")), file_(true) {
// open file - raise exception if cannot open
}
~logstream() {
// check if file and if so close
if(file_) {
os->close();
delete os;
}
}
private:
std::ostream* os;
bool file_;
};
但是我在第 7 行收到此错误:
error C2512: 'std::basic_ostream<_Elem,_Traits>' : no appropriate default constructor available
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
第 9 行也有同样的错误:
error C2512: 'std::basic_ostream<_Elem,_Traits>' : no appropriate default constructor available
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
我还需要在 os->close 行上修复此错误:
error C2039: 'close' : is not a member of 'std::basic_ostream<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
任何想法如何解决这些编译错误?
更新
所以我可以这样做:
class logstream
{
public:
logstream() : os(&std::cout), file_(false) {}
logstream(const char* filename) : os(new std::ofstream("file.txt")), file_(true) {
// open file - raise exception if cannot open
}
~logstream() {
// check if file and if so close
if(file_) {
static_cast<std::ofstream*>(os)->close();
delete os;
}
}
private:
std::ostream* os;
bool file_;
};
使用内部 ostream 对象。 或者我可以从 ostream 继承并删除内部 ostream ptr。 但是我将如何为继承版本编写构造函数呢?
我在这个版本上收到错误:
class logstream2 : public std::ostream
{
public:
logstream2() : std::ostream(std::cout) {}
logstream2(const char* filename) : std::ostream(std::ofstream("file.txt")) {
// open file - raise exception if cannot open - check state of stream
}
~logstream2() {
// not sure if I need this
}
};
错误:
error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 9.0vcincludeios(151) : see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function 'std::basic_ostream<_Elem,_Traits>::basic_ostream(const std::basic_ostream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
这就是我最终得到的。
#include <iostream>
#include <fstream>
class logstream
{
public:
logstream() : os(&std::cout), file_(false) {}
logstream(const char* filename) : os(new std::ofstream(filename)), file_(true) {
// open file - raise exception if cannot open - TODO
}
~logstream() {
// check if file and if so close
if(file_) {
static_cast<std::ofstream*>(os)->close();
delete os;
}
}
template<typename T>
std::ostream& operator<<(const T& o) {
return *os << o;
}
private:
std::ostream* os;
bool file_;
};
int main () {
logstream log("logtest.txt");
log << "Hi Guys!n";
log << "number: " << 3 << std::endl;
return 0;
}
这个呢?
#include <iostream>
#include <ostream>
#include <fstream>
#include <string>
#include <memory>
class logstream
{
public:
logstream() : os(new std::ostream(std::cout.rdbuf())), file_(false) {} // line 7
logstream(const std::string &filename) :
os(new std::ofstream(filename)), file_(true)
{
// open file - raise exception if cannot open
}
std::ostream &operator<<(const std::string &s)
{
*os << s;
}
~logstream()
{
// check if file and if so close
if (file_)
{
dynamic_cast<std::ofstream*>(os.get())->close();
}
}
private:
std::unique_ptr<std::ostream> os;
bool file_;
};
int main()
{
logstream l("test.txt");
l << "some test"; // write to test.txt
logstream another;
another << "another test"; //write to std::cout by default
}
您需要在初始值设定项列表中调用从中继承的类的构造函数 std::ostream
。
相关文章:
- 没有用于初始化C++中的变量模板的匹配构造函数
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- "const wchar_t *" 类型的值不能用于初始化类型 "const PWSTR" 的实体
- 没有用于初始化模拟工厂的匹配构造函数
- 模板 ctor 类型推导不起作用(没有匹配的构造函数用于初始化 ...)与函数<>参数
- 制作可用于初始化向量的迭代器
- 回调方法显示错误,类型为"void(*)"的值不能用于初始化类型的实体
- 用于初始化结构的 void 指针强制转换
- 错误:没有用于初始化和mexfunction的匹配构造函数
- 没有用于初始化可变参数模板类的匹配构造函数
- 类型值不能用于初始化类型实体
- ifstream 没有用于初始化的匹配构造函数
- 用于初始化类内动态数组的函数
- C++17 用于初始化模板类的折叠表达式的极限类型
- VSCod 中的C++:错误:没有用于初始化'std::thread'的匹配构造函数
- 没有用于初始化"ifStream"的匹配构造函数
- 没有用于初始化 std::lock_guard<std::mutex 的匹配构造函数>
- 没有用于初始化我的自定义分配器的匹配构造函数
- 没有用于初始化'point'的匹配构造函数
- C++11 cmake O3 选项<没有用于初始化 'std::thread' 的匹配构造函数>