遇到我所做的自定义日志记录系统的问题。我在我的主文件中声明了一个ofstream,以便它可以在我的类中用静态函数访问。这适用于我的静态功能(ilra_log_enabled
)。但是,这对我的类的重载函数不起作用。我收到“日志文件”错误的未定义引用。对外部变量的未定义引用
任何想法?
#ifndef ILRA_H_
#define ILRA_H_
// System libraries
#include <iostream>
#include <ostream>
#include <sstream>
#include <iomanip>
#include <fstream>
// Namespace
using namespace std;
// Classes
class ilra
{
static int ilralevel_set;
static int ilralevel_passed;
static bool relay_enabled;
static bool log_enabled;
static ofstream logfile;
public:
// constructor/destructor
ilra(const std::string &funcName, int toset)
{
// we got passed a loglevel!
ilralevel_passed = toset;
}
~ilra(){};
static void ilra_log_enabled(bool toset){
log_enabled = toset;
if (log_enabled == true){
// get current time
time_t rawtime;
time (&rawtime);
// name of log file
string logname = "rclient-";
logname.append(rawtime + ".txt");
// open a log file
logfile.open(logname.c_str());
}
}
// output
template <class T>
ilra &operator<<(const T &v)
{
if(ilralevel_passed <= ilralevel_set)
std::cout << v;
if(log_enabled == true)
logfile << "Test"; // undefined reference to ilra::logfile
return *this;
}
}; // end of the class
#endif /* ILRA_H_ */
你为什么不把'logfile'声明为你的类的静态成员? – 2010-07-06 07:19:19
@Vijay,我已经提前修改了程序结构,以便所有变量都是该类的成员。这解决了这个问题。由于之前的问题,我使用这种其他组织风格来封装所有变量。 – BSchlinker 2010-07-06 08:52:31