做一些重构,我在一类移动一些代码后得到一个错误成员函数:错误与ostream的定义
std::ostream logStream; // <-- error
std::filebuf fileBuffer;
// Send the output either to cout or to a file
if(sendToCout) {
logStream.rdbuf(std::cout.rdbuf());
}
else {
fileBuffer.open("out.txt", std::ios_base::out | std::ofstream::app);
logStream.rdbuf(&fileBuffer);
}
logStream << "abcd..." << std::endl;
这是编译器错误消息:
file.cpp:417: error: calling a protected constructor of class 'std::__1::basic_ostream<char>'
std::ostream logStream;
^
难道这是一个办法?
std::filebuf fileBuffer;
std::ostream logStream(&fileBuffer);
...
if(sendToCout) {
logStream.rdbuf(std::cout.rdbuf());
}
检查[可用构造函数调用](http://en.cppreference.com/w/cpp/io/basic_ostream/basic_ostream)。你需要传递'fileBuffer'指针。 –