【发布时间】:2011-03-01 17:43:08
【问题描述】:
我有一个链接器错误,我已简化为一个简单的示例。 构建输出是:
debug/main.o:在函数
main':log& 日志::运算符 collect2: ld 返回 1 退出状态
C:\Users\Dani\Documents\Projects\Test1/main.cpp:5: undefined reference to
链接器似乎忽略了 log.cpp 中的定义。
我也不能将定义放在 log.h 中,因为我多次包含该文件并且它抱怨重新定义。
main.cpp:
#include "log.h"
int main()
{
log() << "hello";
return 0;
}
log.h:
#ifndef LOG_H
#define LOG_H
class log
{
public:
log();
template<typename T>
log &operator <<(T &t);
};
#endif // LOG_H
log.cpp:
#include "log.h"
#include <iostream>
log::log()
{
}
template<typename T>
log &log::operator <<(T &t)
{
std::cout << t << std::endl;
return *this;
}
【问题讨论】: