Possible Duplicate:
Why do I get “unresolved external symbol” errors when using templates?
Why can templates only be implemented in the header file?类方法来创建模板
也许我对于它只是太傻,但是这并没有为我工作:
foo.h中
#ifndef FOO_H
#define FOO_H
class Foo {
public:
template<typename T>
void foo(T const &);
};
#endif // FOO_H
富.cpp
#include "Foo.h"
template<typename T>
void Foo::foo(T const &var) {
// do something useless
}
的main.cpp
#include "Foo.h"
int main() {
int i;
Foo bar;
bar.foo(i);
}
我只是在Xcode 4.4.1编译但它返回我下面的链接错误:
Undefined symbols for architecture x86_64:
"void Foo::foo<int>(int const&)", referenced from:
_main 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)
感谢您的答复!
我们*字面上*刚刚有[这个非常相同的问题](http://stackoverflow.com/q/12446875/596781)...它的模板周日再次:-) –