【问题标题】:calling a function template from a second class从第二个类调用函数模板
【发布时间】:2015-07-07 16:43:54
【问题描述】:

我正在尝试从另一个类中调用在一个类中定义的函数模板,但我卡住了。
我得到错误:

1>class1.obj : error LNK2019: riferimento al simbolo esterno "public: void __thiscall class2::output<double,long>(double,long,float)" (??$output@NJ@class2@@QAEXNJM@Z) non risolto nella funzione "public: void __thiscall class1::x(class class2 &)" (?x@class1@@QAEXAAVclass2@@@Z)

1>class1.obj : error LNK2019: riferimento al simbolo esterno "public: void __thiscall class2::output<float,int>(float,int,float)" (??$output@MH@class2@@QAEXMHM@Z) non risolto nella funzione "public: void __thiscall class1::y(class class2 &)" (?y@class1@@QAEXAAVclass2@@@Z)

1>C:\xxx.exe : fatal error LNK1120: 2 esterni non risolti

如果我取消注释具有重复功能的行(没有模板的行),那没关系。 你能帮忙修一下吗?

文件:“class1.h”

#ifndef CLASS1
#define CLASS1
#include "class2.h"
class class1{
public:
void x(class2& c );
void y(class2& c );
};
#endif

文件:“class1.cpp”

#include "class1.h"
void class1::x( class2& c )
{
double img;
long integ;
float y;
c.output(img, integ, y);
}
void class1::y(class2& c )
{
float img;
int integ;
float y;
c.output(img, integ, y);
}

文件:“class2.h”

#ifndef CLASS2
#define CLASS2
class class2{
void output2(double img, long integ, float y);
void output2(float img, int integ, float y);
public:
template <typename T1, typename T2>
void output(T1 img, T2 integ, float y);
//void output(double img, long integ, float y);
//void output(float img, int integ, float y);
};
#endif

文件:“class2.cpp”

#include "class2.h"
template <typename T1, typename T2>
void class2::output(T1 img, T2 integ, float y)
{output2(img, integ, y);}
//void class2::output(double img, long integ, float y)
//{output2(img, integ, y);}
//void class2::output(float img, int integ, float y)
//{ output2(img, integ, y);}
void class2::output2(double img, long integ, float y){/*...*/}
void class2::output2(float img, int integ, float y){/*...*/}

编辑

我说的是函数模板,而不是类模板。我以前看过我要复制的问题,但这不是我要找的。 我在一个类中有两个函数,除了它们的参数类型之外,它们是相同的。

我只想要一个简单的技巧来避免为同一个类中的两个函数编写和维护相同的代码。 无论如何我找到了一个解决方案,在 class2.cpp 的底部添加这两行:

template void class2::output<double, long>(double img, long integ, float y);
template void class2::output<float, int>(float img, int integ, float y);

【问题讨论】:

  • 我的问题已被否决并标记为重复,但在我看来,最初的问题是关于模板类(或结构)而不是模板成员函数。无论如何,我尝试将 void output(T1 img, T2 integ, float y) 的代码放在头文件中,错误并没有消失。
  • 标记为“重复”已删除,但不反对。

标签: c++ templates


【解决方案1】:

如果您想要专业化,您需要在 .h 文件中实现“默认”模板函数并定义您要实现的专业化。在 .cpp 文件中,您只需实现特化。

文件:“class2.h”

#ifndef CLASS2
#define CLASS2
class class2{
    public:
        template <typename T1, typename T2>
        void class2::output(T1 img, T2 integ, float y)
        { /* .. do stuff.. */}
};
template <>
void class2::output(double img, long integ, float y);
#endif

文件:“class2.cpp”

#include "class2.h"
template <>
void class2::output(double img, long integ, float y)
{ /* .. do specialized stuff.. */}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-22
    • 1970-01-01
    • 2021-03-30
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 2015-09-22
    相关资源
    最近更新 更多