Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.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++,我正在与外部模板进行斗争。与C语言相反,整个模板实现在C++中是非常讨厌的:()_C++ - Fatal编程技术网

外部模板c+出现问题+; 我使用C++,我正在与外部模板进行斗争。与C语言相反,整个模板实现在C++中是非常讨厌的:()

外部模板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; }

模板测试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();
模板
类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。在我看来,它提供了最大的灵活性。:)通常我也在使用它,但有时您有一个非常大的模板类,然后我更喜欢使用第三种解决方案。很高兴能帮上忙:)