c++类模板的显式实例化无效的问题(c++基础)

一个自闭的沙雕 2019-10-17 02:39:46
这里为什么会显式实例化无效呢? 代码如下: #pragma once #include<iostream> #include<cstdlib> template <typename T,int n> class Arraytp { T ar[n]; public: Arraytp() {}; explicit Arraytp(const T& v); virtual T& operator[](int i); virtual T operator[](int i) const; ~Arraytp(); }; template class Arraytp<std::string, 100> { std::string ar[100]; public: Arraytp() {}; explicit Arraytp(const std::string& v); virtual std::string& operator[](int i); virtual std::string operator[](int i) const; ~Arraytp(); }; template<class T, int n> inline Arraytp<T, n>::Arraytp(const T & v) { for (int i = 0; i < n; i++) ar[i] = v; } template<class T, int n> inline T & Arraytp<T, n>::operator[](int i) { if (i < 0 || i >= n) { std::cerr << "数组大小超出范围"; std::exit[EXIT_FAILURE]; } return ar[i]; } template<class T, int n> inline T Arraytp<T, n>::operator[](int i) const { if (i < 0 || i >= n) { std::cerr << "数组大小超出范围"; std::exit[EXIT_FAILURE]; } return ar[i]; } template<class T, int n> inline Arraytp<T, n>::~Arraytp() { }
...全文
858 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 3 楼 提及的回复:

template<>
class Array<int>{};
这个是正解,书上写错了!虽然已经解决了。 给分咯。
提及 2019-10-17
  • 打赏
  • 举报
回复

template<>
class Array<int>{};
喜欢做我自己 2019-10-17
  • 打赏
  • 举报
回复
显示具体化的时候你加class干嘛,那是表明是类,你换成类名::
喜欢做我自己 2019-10-17
  • 打赏
  • 举报
回复
template<class T, int n> template Arraytp::Arraytp<std::string, 100> { std::string ar[100]; public: Arraytp() {}; explicit Arraytp(const std::string& v); virtual std::string& operator[](int i); virtual std::string operator[](int i) const; ~Arraytp(); };

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧