遇到我所做的自定義日誌記錄系統的問題。我在我的主文件中聲明瞭一個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