类模版 在这题中怎么把成员函数正确定义在类外 谢谢了

2025-01-06 04:23:40
推荐回答(3个)
回答1:

是这样吗,试试看:
#include
using namespace std;

template
class Point
{
public:
Point(numtype a,numtype b=0);
void print();
private:
numtype x,y;
};

template
Point::Point(numtype a,numtype b)
{
x=a;
y=b;
}

template
void Point::print()
{
cout<}

int main()
{
Point p(2);
p.print();
return 0;

}

回答2:

是这样吗,试试看:
#include
using
namespace
std;
templatenumtype>
class
Point
{
public:
Point(numtype
a,numtype
b=0);
void
print();
private:
numtype
x,y;
};
templatenumtype>
Point::Point(numtype
a,numtype
b)
{
x=a;
y=b;
}
templatenumtype>
void
Point::print()
{
cout<"<}
int
main()
{
Point
p(2);
p.print();
return
0;
}

回答3:

template Point::print()
{
cout<}