没有主模板,所以编译报错
#include "killCmake.h"
#include<string>
using namespace std;
template<typename T>
class Point<char*, T> // 编译,报错error:主模板的声明中不允许使用模板参数列表
{
public:
Point(char* x, T y) : x_(x), y_(y) {
}
public:
char* getX() const
{
return x_;
}
void setY(char* x)
{
x_ = x;
}
T getY() cosnt
{
return y_;
}
void setY(T y)
{
y_ = y;
}
void display() const
{
std::cout << "x_: " << x_ << std::endl;
std::cout << "y_: " << y_ << std::endl;
}
private:
char* x_;
T y_;
};
int main()
{
return 0;
}
添加主模板,解决错误: 主模板的声明中不允许使用模板参数列表
#include "killCmake.h"
#include<string>
using namespace std;
// 添加主模板
template<typename T,typename TT>
class Point;
template<typename T>
class Point<char*, T> // 编译,报错error:主模板的声明中不允许使用模板参数列表
{
public:
Point(char* x, T y) : x_(x), y_(y) {
}
public:
char* getX() const
{
return x_;
}
void setY(char* x)
{
x_ = x;
}
T getY() const
{
return y_;
}
void setY(T y)
{
y_ = y;
}
void display() const
{
std::cout << "x_: " << x_ << std::endl;
std::cout << "y_: " << y_ << std::endl;
}
private:
char* x_;
T y_;
};
int main()
{
return 0;
}
遇到说c++成员函数声明为const是鸡肋的,敬而远之,我前面一篇专门讲过,const类型的成员函数修改了类中this指针的指向类型,提高了成员函数的调用灵活性,这是实打实的语法功能,绝不是什么鸡肋,我的模板编程系列正在写,有些工作很多年的面试者说我们代码是为了写的简单易懂所以不用模板,不写元编程,我面试过程中晕过去,就事论事,不用模板,在标准库重写过程中寸步难行。
永生追随开源及分享精神,不断向他人学习,并乐意分享