C++ 在类(C+;+;)中使用模板时未声明成员函数
我在代码中使用了以下类C++ 在类(C+;+;)中使用模板时未声明成员函数,c++,C++,我在代码中使用了以下类 class pos2d { float x, y; public: float getx() const; float gety() const; //int getz() const; //How to avoid?? }; class pos3d { float x, y, z; public: float getx() const; float gety() const;
class pos2d
{
float x, y;
public:
float getx() const;
float gety() const;
//int getz() const; //How to avoid??
};
class pos3d
{
float x, y, z;
public:
float getx() const;
float gety() const;
float getz() const;
};
template<class T, class P>
class entity
{
T type;
P position;
public:
void print() const;
};
类pos2d
{
浮动x,y;
公众:
float getx()常量;
float gety()常量;
//int getz()const;//如何避免??
};
类pos3d
{
浮动x,y,z;
公众:
float getx()常量;
float gety()常量;
float getz()常量;
};
模板
类实体
{
T型;
P位;
公众:
无效打印()常量;
};
我在类实体中的打印函数如下
template<class T, class P>
void entity<T, P>::print() const
{
if (type == 1 || type == 'A')
cout << "Object is of type " << type << " and has coordinates as (" << position.getx() << ", " << position.gety() << ", " << position.getz() << ")\n\n"; //Avoid getz in pos2d
else if (type == 2 || type == 'B')
cout << "Object is of type " << type << " and has coordinates as (" << position.getx() << ", " << position.gety() << ")\n\n";
}
模板
void实体::print()常量
{
如果(类型==1 | |类型==A)
我可以看到两种简单的方法来实现这一点:
-使print()成为posX类的成员函数。
-在实体类中专门化print()函数。
#include <iostream>
class pos2d
{
float x, y;
public:
float getx() const { return x; }
float gety() const { return y; }
void print() const
{
std::cout << "Object is of type and has coordinates as (" << getx() << ", " << gety() << ")\n\n";
}
};
class pos3d
{
float x, y, z;
public:
float getx() const { return x; }
float gety() const { return y; }
float getz() const { return z; }
void print() const
{
std::cout << "Object is of type and has coordinates as (" << getx() << ", " << gety() << ", " << getz() << ")\n\n";
}
};
template <class P>
class entity
{
P position;
public:
void print() const { position.print(); }
void print_specialized() const;
};
template <>
void entity<pos3d>::print_specialized() const {
std::cout << "Object is of type and has coordinates as (" << position.getx() << ", " << position.gety() << ", " << position.getz() << ")\n\n";
}
template <>
void entity<pos2d>::print_specialized() const {
std::cout << "Object is of type and has coordinates as (" << position.getx() << ", " << position.gety() << ")\n\n";
}
int main() {
entity<pos2d> e;
e.print();
e.print_specialized();
entity<pos3d> e2;
e2.print();
e2.print_specialized();
return 0;
}
#包括
类pos2d
{
浮动x,y;
公众:
float getx()常量{return x;}
float gety()常量{return y;}
void print()常量
{
std::cout问题在于,如果将pos2d
作为模板参数传递给行
cout << "Object is of type " << type << " and has coordinates as (" << position.getx() << ", " << position.gety() << ", " << position.getz() << ")\n\n";
检索2D向量(或点)的<代码> z < /代码>坐标是没有意义的。考虑使用sFIAE。查看关于<代码> STD::Enable的文档,如果
template<class P>
class entity
{
P position;
public:
void print() const;
};
template<>
void entity<pos3d>::print() const
{
cout << "Object is of type pos3d and has coordinates as ("
<< position.getx() << ", " << position.gety() << ", " << position.getz() << ")\n\n";
}
template<>
void entity<pos2d>::print() const
{
cout << "Object is of type pos2d and has coordinates as ("
<< position.getx() << ", " << position.gety() << ")\n\n";
}