关于类模板中定义一个静态变量[新手]

blueswindxxj blueswindxxj 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
作用是要完成VB中collection一样的一个池,可以在其中放入对象或各种类型的变量.
我的初步思路是用二个CLASS建立(Collection_class and Collection),一个CLASS(Collection_class)用做存放传放的对象,另一个(Collection)去记录Collection_class的存放地址,用户通过Collection中的方法去找Collection_class中存放的对象,不知道这样的思路对不对!~

在实现时遇到了一个问题:因为是模板,在定义STATIC变量初始化时是老是出错.试了很多次,未果!~望高手帮助给点提示什么的,谢谢!~

此程序还没写完,如果有看不明白的还请多多指导.....

#include <iostream>

using namespace std;

template <class Newtype>
class Collection_class
{
private:
Newtype E; //需要放入的对象
Collection_class *Next; //指向自己的指针
public:
Collection_class()
{
Next=NULL;
}
void Add(Newtype e)
{
E=e;
}
Newtype Get()
{
return E;
}
};

template <class Addtype>
class Collection
{
protected:
Collection_class<Addtype> Head;
Collection_class<Addtype> Temp;
public:
static Collection_class<Addtype> *Last; //定义一个静态的LAST指针记录上一个Collection_class对象的地址,方便放入当前的Collection_class *Next中
void Add(Addtype i)
{
if (Last=NULL)
{
Head.Add(i); //调用Collection_class中的ADD方法存放对象
Last=&Head;
}
else
{
Temp.Add(i);
}
}
Addtype Get()
{
return Head.Get();
}
};

//************** 出 错 处 **********************
template <class Newtype,class Addtype>
Collection_class<Newtype> Collection<Addtype>::Last=NULL; //静态变量初始化,,此为出错的地方,也许不应该这样写......

void main()
{
Collection<double> a;
a.Add(0.12345);
cout<<a.Get()<<endl;
}
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
blueswindxxj
C++ 语言 65,214 成员 250,514 内容