【发布时间】:2015-07-12 21:19:36
【问题描述】:
以下代码是重现我的问题的最少代码。当我尝试编译它时,链接器找不到operator== for Config:
Undefined symbols for architecture x86_64:
"operator==(Config<2> const&, Config<2> const&)", referenced from:
_main in test2.o
operator== 是Config 的朋友。但是当我不再将operator== 声明为朋友时,代码编译器没有错误。
template <int DIM>
class Config{
// comment the following line out and it works
friend bool operator==(const Config<DIM>& a, const Config<DIM>& b);
public:
int val;
};
template <int DIM>
bool operator==(const Config<DIM>& a, const Config<DIM>& b){
return a.val == b.val;
}
int main() {
Config<2> a;
Config<2> b;
a == b;
return 0;
}
这里有什么问题?
【问题讨论】:
标签: c++ templates linker friend