我在看一個library on github其在頭文件下列操作之一:如何在源文件中定義一個類並在頭文件中聲明它(而不必使用`class :: method`語法來定義類方法)?
class Util
{
public:
static void log(const string& message);
static void log(const char* message);
template<typename T>
static inline string toString(T t) { stringstream s; s << t; return s.str(); }
};
和源文件中的以下內容:
void Util::log(const string& message)
{
const string& logMessage = "[cppWebSockets] " + message;
syslog(LOG_WARNING, "%s", logMessage.c_str());
}
void Util::log(const char* message)
{
log(string(message));
}
爲什麼,當我替換上面源文件中的內容與下面的編譯器抱怨「重新定義」?我認爲頭文件只包含聲明,而源文件實際上定義了類?
class Util
{
public:
void Util::log(const string& message)
{
const string& logMessage = "[cppWebSockets] " + message;
syslog(LOG_WARNING, "%s", logMessage.c_str());
}
void Util::log(const char* message)
{
log(string(message));
}
}
如何使用上述樣式定義類而不是Util::log
?