小编JPA*_*M69的帖子

LNK2019(VS 2008)使用模板函数指针完全实现模板函数

以下最小代码在GNU C++中编译和链接很好:

#include <iostream>

// Simple function
template<class T>
void foo(T a,void* = 0) {
  std::cout << a << std::endl;
}

// A distpatching class
template<
         class T,
         void (*Function)(T,void*)
        >
class kernel {
public:
  // Function dispatcher
  template<class M>
  inline static void apply(M t) {
    Function(t,0);
  }
};

int main()
{
  kernel<int,foo>::apply(5);
  //foo(5,0);
}
Run Code Online (Sandbox Code Playgroud)

但是使用Visual Studio 2008会产生错误

error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""void __cdecl foo<int>(int,void *)" (??$foo@H@@YAXHPAX@Z)" in Funktion ""public: static void __cdecl kernel<int,&void __cdecl foo<int>(int,void …
Run Code Online (Sandbox Code Playgroud)

c++ templates function-pointers lnk2019 visual-studio-2008

8
推荐指数
1
解决办法
443
查看次数