我正在尝试制作一个记录到 std::cout 和文件的记录器。这是我的课:
.h 文件:
class ProblemsManager {
(...)
private:
(...)
class logger {
private:
std::ofstream fileStream;
static const std::string LOG_PATH;
public:
logger();
~logger();
template<class T>
friend logger & operator<<(logger &log, const T & bytes) {
log.fileStream<<bytes;
std::cout<<bytes;
return log;
}
};
};
.cpp 文件
(...)
const std::string ProblemsManager::logger::LOG_PATH = "F:\\Dropbox\\workspace - Visual Studio\\PEuler\\PEuler\\PEuler.log";
ProblemsManager::logger::logger() : fileStream(LOG_PATH,std::ofstream::out) {}
ProblemsManager::logger::~logger() {}
然后,如果我尝试这样做:
ProblemsManager::logger log;
log<<"test";
我得到:
1>f:\dropbox\workspace - visual studio\peuler\peuler\problemsmanager.cpp(47): error C3767: '<<': 候选函数不可访问 1> 可能是 'f:\ dropbox\workspace - visual studio\peuler\peuler\problemsmanager.h(37)' : '<<' [可以通过参数相关查找找到]