Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板方法中未解析的外部符号_C++_Templates_Methods_Lnk2019 - Fatal编程技术网

C++ 模板方法中未解析的外部符号

C++ 模板方法中未解析的外部符号,c++,templates,methods,lnk2019,C++,Templates,Methods,Lnk2019,这将是一个有点复杂,我解释没有张贴大量的代码,但我会尽我最大的努力。我正在编写一个游戏中的控制台,它要求我将普通的免费函数和实例的方法绑定到控制台,这样就可以使用简单的关键字调用它们。控制台可以绑定到的每个方法/函数都需要一个sf::String参数,并返回一个sf::String。(我在游戏中使用SFML,sf是SFML的名称空间) [注意:所有4个类(Console、CFunctionInterface、CFunction和CMethod)都在同一个文件(Console.h)中声明。它们的成

这将是一个有点复杂,我解释没有张贴大量的代码,但我会尽我最大的努力。我正在编写一个游戏中的控制台,它要求我将普通的免费函数和实例的方法绑定到控制台,这样就可以使用简单的关键字调用它们。控制台可以绑定到的每个方法/函数都需要一个sf::String参数,并返回一个sf::String。(我在游戏中使用SFML,sf是SFML的名称空间)

[注意:所有4个类(Console、CFunctionInterface、CFunction和CMethod)都在同一个文件(Console.h)中声明。它们的成员都在单个文件(Console.cpp)中定义]

为了进行设置,我有1个父类(CFunctionInterface)和2个子类(CFunction,CMethod)“container”类。父级中有1个公共方法

sf::String call(sf::String p_Value);
CFunction的声明如下所示:

class CFunction : public CFunctionInterface
{
public:
    typedef sf::String (*FFP)(sf::String);  //free function pointer type

    CFunction(const FFP p_Function);
    sf::String call(const sf::String p_Value);
private:
    FFP m_Function;
};
template <typename ClassType>
class CMethod : public CFunctionInterface
{
public:
    typedef sf::String (ClassType::*MFP)(sf::String);   //member function pointer type

    CMethod(const ClassType* p_Instance, const MFP p_Function);
    sf::String call(const sf::String p_Value);
private:
    ClassType* m_Instance;
    MFP m_Function;
};
CMethod的声明如下所示:

class CFunction : public CFunctionInterface
{
public:
    typedef sf::String (*FFP)(sf::String);  //free function pointer type

    CFunction(const FFP p_Function);
    sf::String call(const sf::String p_Value);
private:
    FFP m_Function;
};
template <typename ClassType>
class CMethod : public CFunctionInterface
{
public:
    typedef sf::String (ClassType::*MFP)(sf::String);   //member function pointer type

    CMethod(const ClassType* p_Instance, const MFP p_Function);
    sf::String call(const sf::String p_Value);
private:
    ClassType* m_Instance;
    MFP m_Function;
};
现在,我在控制台中有一些方法,用于绑定和取消绑定函数。这就是问题所在

template <typename ClassType>
void bindFunction(const string name, const ClassType* instance, sf::String (ClassType::*func)(sf::String));
void bindFunction(const string name, sf::String (*func)(sf::String));
void unbindFunction(const string name);
模板
void bindFunction(常量字符串名称,常量类类型*实例,sf::string(类类型::*func)(sf::string));
void bindFunction(常量字符串名称,sf::string(*func)(sf::string));
void unbind函数(常量字符串名称);
最后两个函数工作得很好。没问题。但就是那个模板混蛋。 以下是他的定义(在console.cpp中):

模板
void CConsole::bindFunction(常量字符串名称、常量类类型*实例、sf::string(类类型::*func)(sf::string))
{
//检查名称是否已在使用中
for(auto-iter=functions.begin();iter!=functions.end();iter++)
{
如果(iter->first==名称)
{
向后推(“错误!尝试绑定函数。名称\'“+name+”\”已在使用中!”);
返回;
}
}
//一切正常
CMethod*temp=新的CMethod(实例,func);
插入(对(名称,临时));
}
当我在整个程序中都不给他打电话时,它的编译和运行都很好。但当我试着叫它;我得到这个错误:

1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall CConsole::bindFunction<class CDog>(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class CDog const *,class sf::String (__thiscall CDog::*)(class sf::String))" (??$bindFunction@VCDog@@@CConsole@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PBVCDog@@P83@AE?AVString@sf@@V45@@Z@Z) referenced in function _main
1>S:\Users\Logan\documents\visual studio 2012\Projects\newConsole\Debug\newConsole.exe : fatal error LNK1120: 1 unresolved externals
1>main.obj:error LNK2019:未解析的外部符号“public:void u thiscall CConsole::bindFunction(类std::basic_string,类CDog const*,类sf::string(uu thiscall CDog:*)(类sf::string))”($bindFunction@VCDog@@@C解决方案@@QAEXV?$basic_string@DU?$char_traits@D@性病病毒$allocator@D@2@@std@@PBVCDog@@P83@AE?AVString@sf@@V45@@Z@Z)在函数_main中引用
1> S:\Users\Logan\documents\visual studio 2012\Projects\newConsole\Debug\newConsole.exe:致命错误LNK1120:1未解析的外部
这就是在“int main()”中调用它的方式:

CDog*rex=新的CDog;
控制台->绑定功能(“树皮”、rex和CDog::树皮);
CDog只是一个简单的类,带有返回“Woof!”的public bark()方法。 控制台是在main内部创建的,只是指向新CConsole对象的指针。 包含“console.h”,并且没有#include循环。我查过了

我在网上做过一些谷歌搜索,但我从未发现像这样的链接器错误。它总是关于忘记一个include或拥有一个include循环


有人知道我做错了什么,以及如何纠正吗?感谢您抽出时间阅读此文章,并提前感谢您的回复。-Logan

仅仅在源文件中有一个模板成员函数是可疑的。哦,我还建议您查找和(或它们的增强)可疑如何?我试着把它移到.h,等等,但是我一直得到同样的错误。当你使用一个类时,它必须被完全定义。对于模板化类,这意味着所有成员函数也必须对类的用户可见,这意味着所有成员函数都应该与类一起位于头文件中。现在所有内容都在.h中,我不再收到链接器错误,但我收到了大量其他错误。
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall CConsole::bindFunction<class CDog>(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class CDog const *,class sf::String (__thiscall CDog::*)(class sf::String))" (??$bindFunction@VCDog@@@CConsole@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PBVCDog@@P83@AE?AVString@sf@@V45@@Z@Z) referenced in function _main
1>S:\Users\Logan\documents\visual studio 2012\Projects\newConsole\Debug\newConsole.exe : fatal error LNK1120: 1 unresolved externals
CDog* rex = new CDog;
console->bindFunction<CDog>("bark", rex, &CDog::bark);