gpt4 book ai didi

c++ - operator== 是友元时的链接器错误

转载 作者:搜寻专家 更新时间:2023-10-31 00:12:28 25 4
gpt4 key购买 nike

<分区>

以下代码是重现我的问题的最少代码。当我尝试编译它时,链接器找不到 Configoperator==:

Undefined symbols for architecture x86_64:
"operator==(Config<2> const&, Config<2> const&)", referenced from:
_main in test2.o

operator==Config 的 friend 。但是当我不再将 operator== 声明为 friend 时,代码编译器没有错误。

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;
}

这里有什么问题?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com