在main.cpp
我有
#include "helper.h"
#include <list>
using namespace std;
int main(int argc, const char * argv[]) {
//switch between list and vector
list<int> a = {0, 1};
list<char> b = {'a', 'b'};
func(a, b);
return 0;
}
我的helper.h
看起来像
template <class C1, class C2>
void func(C1 &a, C2 &b)
我的helper.cpp
看起来像
#include <list>
#include "helper.h"
template <class C1, class C2>
void func (C1 &a, C2 &b)
{
//do stuff
}
然后使用g++ main.cpp mysort.cpp -o sorting -std=c++11
在Mac上的终端中构建它会生成错误"function func has internal linkage but is not defined"
和Undefined symbols for architecture x86_64:
"void func<std::__1::list<char, std::__1::allocator<char> >, std::__1::list<int, std::__1::allocator<int> >, main::$_0>(std::__1::list<char, std::__1::allocator<char> >&, std::__1::list<int, std::__1::allocator<int> >&, main::$_0)", referenced from:
_main in main-202a16.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)