一个模板的模板容器 实例化问题(高分求教)

threeleafzerg007 2009-04-30 11:48:30
没咋写过泛型的东西,所以出了些编译错误。

代码如下: (高分请高手)

#include <map>
#include <iostream>
#include <stdlib.h>
using namespace std;

template<typename ACE_LOCK,
typename K,
typename V,
template<typename Key,typename Value,typename = less<Key>,typename = std::allocator<std::pair<const Key,Value> >
class Cont = map >
class CMEMContainer {

public:
CMEMContainer() {};
virtual V* allocate(const K& rhs)
{

void *ptr;
V *elem;
typename Cont<K,V*>::iterator it;

it = this->container_.find(rhs);
if(it != this->container_.end())
{
ptr = (*it).second;
}
else
{
ptr = (void*)malloc(sizeof(V));
}

if(NULL == ptr)
{
return ptr;
}

elem = new (ptr) V;

if(it == this->container_.end())
this->container_.insert(make_pair(rhs,elem));

return elem;
}

virtual void reclaim(const K& rhs,V* ptr)
{
typename Cont<K,V*>::iterator it;

it = this->container_.find(rhs);
if(it != this->container_.end() && (*it).second == ptr)
{
ptr->~V();
}

return;
}

~CMEMContainer()
{
for(typename Cont<K,V*>::iterator it = this->container_.begin(); it != this->container_.end(); ++it)
{
free((*it).second);
}
}
private:
ACE_LOCK lock_;
Cont<K,V*> container_;
};

int main()
{
CMEMContainer<int,int,int,map> i;
}


编译错误 很多。。。 用的g++ 4.3.2

g++ -c -Wall -O2 -I../common/include -I./ -I/opt/include main.cpp -o main.o
In file included from main.cpp:10:
class.h:13: error: two or more data types in declaration of 'type name'
class.h:13: error: expected `>' before '=' token
class.h:13: error: expected `class' before '=' token
class.h:70: error: 'Cont' is not a template
class.h:70: error: field 'container_' has incomplete type
class.h: In member function 'virtual V* CMEMContainer<ACE_LOCK, K, V, <template-parameter-1-4> >::allocate(const K&)':
class.h:23: error: expected initializer before '<' token
class.h:25: error: 'it' was not declared in this scope
class.h: In member function 'virtual void CMEMContainer<ACE_LOCK, K, V, <template-parameter-1-4> >::reclaim(const K&, V*)':
class.h:50: error: expected initializer before '<' token
class.h:52: error: 'it' was not declared in this scope
class.h: In destructor 'CMEMContainer<ACE_LOCK, K, V, <template-parameter-1-4> >::~CMEMContainer()':
class.h:63: error: expected initializer before '<' token
class.h:63: error: 'it' was not declared in this scope
class.h: In destructor 'CMEMContainer<ACE_LOCK, K, V, <template-parameter-1-4> >::~CMEMContainer() [with ACE_LOCK = int, K = int, V = int, <template-parameter-1-4> = std::map]':
main.cpp:29: instantiated from here
class.h:63: error: 'class CMEMContainer<int, int, int, std::map>' has no member named 'container_'
class.h: In member function 'V* CMEMContainer<ACE_LOCK, K, V, <template-parameter-1-4> >::allocate(const K&) [with ACE_LOCK = int, K = int, V = int, <template-parameter-1-4> = std::map]':
main.cpp:55: instantiated from here
class.h:25: error: 'class CMEMContainer<int, int, int, std::map>' has no member named 'container_'
class.h:26: error: 'class CMEMContainer<int, int, int, std::map>' has no member named 'container_'
class.h:37: error: invalid conversion from 'void*' to 'int*'
class.h:42: error: 'class CMEMContainer<int, int, int, std::map>' has no member named 'container_'
class.h:43: error: 'class CMEMContainer<int, int, int, std::map>' has no member named 'container_'
class.h: In member function 'void CMEMContainer<ACE_LOCK, K, V, <template-parameter-1-4> >::reclaim(const K&, V*) [with ACE_LOCK = int, K = int, V = int, <template-parameter-1-4> = std::map]':
main.cpp:55: instantiated from here
class.h:52: error: 'class CMEMContainer<int, int, int, std::map>' has no member named 'container_'
class.h:53: error: 'class CMEMContainer<int, int, int, std::map>' has no member named 'container_'
make: *** [main.o] Error 1

...全文
253 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
threeleafzerg007 2009-05-02
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 Jinhao 的回复:]
貌似有问题,如何区分一个是否被创建了对象。那么析够函数可能会导致内存泄露,如果一个分配的对象没有调用reclaim,那么析构CMEMContainer的时候就出问题了。

C/C++ codetemplate<typename ACE_LOCK,
typename K,
typename V,
template<typename Key,typename Value,typename=less<Key>,typename=std::allocator<std::pair<constKey,Value>>>//最后那个>classCont=map>classCMEMContainer
{structcookie
{boolempty;//表…
[/Quote]

你说的很不错,我的设计是规定 必须 reclaim 配对 allocate

你的设计更加优于我 呵呵 ,谢谢
threeleafzerg007 2009-05-01
  • 打赏
  • 举报
回复
呵呵 已经搞定了 属于 模板的模板吧 可以将最后的那个容器也一同范化
重用性更强,那个容器里的模板参数 是从外面的得到的。
Jinhao 2009-05-01
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 bibingyan 的回复:]
听说要少用非类型参数,为什么呢
[/Quote]

其中的模板参数Cont不是非类型参数。而是模板模板参数,这是一个递归的逻辑

例如,你可以写出更加变态的模板参数
template<template<template<template<typename>class>class>class T> class template_needs_a_suck_template_template_argument{};
T就是所谓的变态的参数

  • 打赏
  • 举报
回复
搞不定,没见过这样用模板的。。
threeleafzerg007 2009-05-01
  • 打赏
  • 举报
回复
自己搞定了

template <typename ACE_LOCK,
typename K,
typename V,
template <typename Key,typename Value,typename = less <Key>,typename = std::allocator <std::pair <const Key,Value> > >
class Cont = map >

红色标记处,漏了一个> 不是后面的 ,不过也谢谢你,呵呵。
Jarrys 2009-05-01
  • 打赏
  • 举报
回复
听说要少用非类型参数,为什么呢
Jarrys 2009-05-01
  • 打赏
  • 举报
回复
模版实际应用中用的多吗?一般用什么地方。。
threeleafzerg007 2009-05-01
  • 打赏
  • 举报
回复
我把 模板参数精简了一下 结果还是有问题


template<typename ACE_LOCK,
typename K,
typename V,
template<typename K,typename V>
class CONT = map>


不过错误少了很多,
charlesye@charlesye-desktop:~/Program/swap$ make
g++ -c -Wall -O2 -I../common/include -I./ -I/opt/include main.cpp -o main.o
main.cpp: In function 'int main()':
main.cpp:29: error: type/value mismatch at argument 4 in template parameter list for 'template<class ACE_LOCK, class K, class V, template<class K, class V> class CONT> class CMEMContainer'
main.cpp:29: error: expected a template of type 'template<class K, class V> class CONT', got 'template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map'
main.cpp:29: error: invalid type in declaration before ';' token
main.cpp:29: warning: unused variable 'i'
make: *** [main.o] Error 1

红色的,我感觉是 关键错误信息,不过我知道怎么解决。。。
threeleafzerg007 2009-05-01
  • 打赏
  • 举报
回复
不是滴 我加了也没用
S_zxing 2009-05-01
  • 打赏
  • 举报
回复
进来学习~
CARL_SEN 2009-05-01
  • 打赏
  • 举报
回复
还真没见过这样使用模板的~ 我脑子有点乱~
Paradin 2009-05-01
  • 打赏
  • 举报
回复
很好
Jinhao 2009-05-01
  • 打赏
  • 举报
回复
貌似有问题,如何区分一个是否被创建了对象。那么析够函数可能会导致内存泄露,如果一个分配的对象没有调用reclaim,那么析构CMEMContainer的时候就出问题了。


template<typename ACE_LOCK,
typename K,
typename V,
template<typename Key,typename Value,typename = less<Key>,typename = std::allocator<std::pair<const Key,Value> > > //最后那个>
class Cont = map >
class CMEMContainer
{
struct cookie
{
bool empty; //表示buf上是否构造了V对象
char buf[sizeof(V)];
};
public:
typedef Cont<K, cookie> cont_t;

CMEMContainer() {};

virtual V* allocate(const K& rhs)
{
cookie * node;

typename cont_t::iterator it = this->container_.find(rhs);
if(it == this->container_.end())
{
cookie ck;
ck.empty = true;
node = &(this->container_.insert(make_pair(rhs, ck)).first->second);
}
else
node = &((*it).second);

if(node->empty)
{
node->empty = false;
return new (node->buf) V;
}

return 0;
}

virtual void reclaim(const K& rhs)
{
typename cont_t::iterator it;

it = this->container_.find(rhs);
if(it != this->container_.end())
{
if(!it->second.empty)
{
reinterpret_cast<V*>(it->second.buf)->~V();
it->second.empty = true;
}
}

return;
}

~CMEMContainer()
{
for(typename cont_t::iterator it = this->container_.begin(); it != this->container_.end(); ++it)
{
if(!it->second.empty)
reinterpret_cast<V*>(it->second.buf)->~V();
}
}
private:
ACE_LOCK lock_;
cont_t container_;
};
shexinwei 2009-05-01
  • 打赏
  • 举报
回复
都是基本的语法问题吧,细心点就可以了!!!
自律则自由 2009-05-01
  • 打赏
  • 举报
回复
牛,我什么时候才能看的懂?
zhangxun2007 2009-05-01
  • 打赏
  • 举报
回复
不懂,帮不上忙了。
lingyin55 2009-05-01
  • 打赏
  • 举报
回复
up,先多谢分享。
[Quote=引用 8 楼 hairetz 的回复:]
引用 7 楼 threeleafzerg007 的回复:
呵呵 已经搞定了 属于 模板的模板吧 可以将最后的那个容器也一同范化
重用性更强,那个容器里的模板参数 是从外面的得到的。


把搞定的贴全分享下,谢谢。
[/Quote]
BuleRiver 2009-05-01
  • 打赏
  • 举报
回复
学习了
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 threeleafzerg007 的回复:]
呵呵 已经搞定了 属于 模板的模板吧 可以将最后的那个容器也一同范化
重用性更强,那个容器里的模板参数 是从外面的得到的。
[/Quote]

把搞定的贴全分享下,谢谢。
liliangbao 2009-04-30
  • 打赏
  • 举报
回复
帮顶~
加载更多回复(1)

65,211

社区成员

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

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