2

首先感谢您抽出宝贵的时间。我对模板有一点问题(我对这个模板很陌生)。

该代码使用 Visual Studio 2012 编译(或至少尝试编译)C++。

主.h:

class Main
{
public:

template<class T>
static void foo(T param1);
};

主文件

#include "main.h"
template<class T> 
static void Main::foo(T param1)
{
  // do things
}

其他.h

#include "main.h"

class Other
{
public:
  void foo2();
};

其他.cpp

#include "other.h"

void Other::foo2()
{
  int var1 = 10;
  Main::foo(var1); // Here is the link error.
}

好吧,您可能知道非常常见的未解决的外部符号的问题,所以我环顾网络以找到可以帮助我理解和解决此链接错误的东西,我发现了一些我已经找到的东西已经尝试但没有结果。

我试过了:

1-在.h文件中实现foo函数

2-使用内联关键字

3-尝试导出(编译器实际上不支持)

但这些方法似乎都不适合我,所以很明显我做错了什么或者我错过了一些东西。

请记住,模板函数必须在“Main”类中声明。尽管可以解决错误,但将函数移到“其他”类对我没有帮助。

错误:

错误 5 错误 LNK2019: 函数“public: void __thiscall ClientManager::RequestLogin(int,char *)" (?RequestLogin@ClientManager@@QAEXHPAD@Z)

其中CGame将是MainPush = foo()Cl​​ientManager = OtherRequestLogin = foo2

感谢你的宝贵时间。

4

0 回答 0