外部模板c+出现问题+; 我使用C++,我正在与外部模板进行斗争。与C语言相反,整个模板实现在C++中是非常讨厌的:()
模板测试hpp外部模板c+出现问题+; 我使用C++,我正在与外部模板进行斗争。与C语言相反,整个模板实现在C++中是非常讨厌的:(),c++,C++,模板测试hpp template<class T> class CFoo { public: T Foo_Func(const T& test); }; #include "Template_Test.hpp" template<class T> T CFoo<T>::Foo_Func(const T& test) { return test; }
template<class T>
class CFoo {
public:
T Foo_Func(const T& test);
};
#include "Template_Test.hpp"
template<class T>
T CFoo<T>::Foo_Func(const T& test)
{
return test;
}
#include "Template_Test.hpp"
extern template class CFoo<int>;
int Template_Tests();
#include "Template_Test2.hpp"
int Template_Tests()
{
CFoo<int> foo_instance;
//this causes an undefined reference
int res = foo_instance.Foo_Func(1);
return res;
}
#include "Template_Test.hpp"
#include "Template_Test.cpp"
/*extern template class CFoo<int>;*/ // Again, you don't need this extern
int Template_Tests();
模板
类CFoo{
公众:
T Foo_Func(常数测试);
};
模板测试.cpp
template<class T>
class CFoo {
public:
T Foo_Func(const T& test);
};
#include "Template_Test.hpp"
template<class T>
T CFoo<T>::Foo_Func(const T& test)
{
return test;
}
#include "Template_Test.hpp"
extern template class CFoo<int>;
int Template_Tests();
#include "Template_Test2.hpp"
int Template_Tests()
{
CFoo<int> foo_instance;
//this causes an undefined reference
int res = foo_instance.Foo_Func(1);
return res;
}
#include "Template_Test.hpp"
#include "Template_Test.cpp"
/*extern template class CFoo<int>;*/ // Again, you don't need this extern
int Template_Tests();
#包括“Template_Test.hpp”
模板
T CFoo::Foo_Func(const T&test)
{
回归试验;
}
模板测试2.hpp
template<class T>
class CFoo {
public:
T Foo_Func(const T& test);
};
#include "Template_Test.hpp"
template<class T>
T CFoo<T>::Foo_Func(const T& test)
{
return test;
}
#include "Template_Test.hpp"
extern template class CFoo<int>;
int Template_Tests();
#include "Template_Test2.hpp"
int Template_Tests()
{
CFoo<int> foo_instance;
//this causes an undefined reference
int res = foo_instance.Foo_Func(1);
return res;
}
#include "Template_Test.hpp"
#include "Template_Test.cpp"
/*extern template class CFoo<int>;*/ // Again, you don't need this extern
int Template_Tests();
#包括“Template_Test.hpp”
外部模板类CFoo;
int模板_测试();
模板测试2.cpp
template<class T>
class CFoo {
public:
T Foo_Func(const T& test);
};
#include "Template_Test.hpp"
template<class T>
T CFoo<T>::Foo_Func(const T& test)
{
return test;
}
#include "Template_Test.hpp"
extern template class CFoo<int>;
int Template_Tests();
#include "Template_Test2.hpp"
int Template_Tests()
{
CFoo<int> foo_instance;
//this causes an undefined reference
int res = foo_instance.Foo_Func(1);
return res;
}
#include "Template_Test.hpp"
#include "Template_Test.cpp"
/*extern template class CFoo<int>;*/ // Again, you don't need this extern
int Template_Tests();
#包括“Template_Test2.hpp”
int模板_测试()
{
CFoo foo_实例;
//这会导致一个未定义的引用
int res=foo_instance.foo_Func(1);
返回res;
}
为什么链接器找不到我的函数。我认为外部模板的工作原理与外部变量的工作原理相同。
(将extern int test;
放入头文件,并将int test=0
放入源文件。)
感谢您的支持:)解决方案1
解决此问题的一种方法是在没有函数定义的情况下实现模板类的函数。在这种情况下:
template<class T>
class CFoo {
public:
T Foo_Func(const T& test) {
return test;
}
};
解决方案3
这是最接近你尝试的方式。在template_test.cpp
文件的末尾,添加以下行:
template class CFoo<int>;
模板类CFoo;
并删除行外部模板类CFoo代码>来自模板_test2.hpp
文件
我希望你会发现它有帮助,科雷尔。相关/重复:你错过了一个模板类CFoo代码>在template\u test.cpp
中。对于这篇非常有用的文章,thx可能是重复的。在考虑了每个解决方案的优缺点后,我决定采用解决方案1。在我看来,它提供了最大的灵活性。:)通常我也在使用它,但有时您有一个非常大的模板类,然后我更喜欢使用第三种解决方案。很高兴能帮上忙:)