gpt4 book ai didi

c++ - 在模板化类中重载 << 运算符会给我链接器错误 (C++)

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

<分区>

在你问之前,是的,该函数是在我的模板类的 header 中定义的。

这是 Example.h 的相关部分:

template<class T, class J, const int X, const int Y>
class Example {
public:
friend std::ostream& operator<<(std::ostream& s, const Example<T,J,X,Y>& b);
}

template<class T, class J, const int X, const int Y>
std::ostream& operator<<(std::ostream& s, const Example<T,J,X,Y>& b) {
// stuff
}

我从 main.cpp 调用它:

void foo(Example<A,B,5,5>& b) {
std::cout << b;
}

int main() {
Example<A,B,5,5> b = Example<A,B,5,5>();
foo(b);
}

编译时出现以下链接器错误:

Undefined symbols for architecture x86_64:
"operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Example<A, B, 5, 5> const&)", referenced from:
foo(Example<A, B, 5, 5>&) in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [dist/Debug/GNU-MacOSX/consolehero] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

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