17 12
发新话题
打印

SOS 紧急求助,各位大哥,帮帮忙吧 (关于 allocator)

SOS 紧急求助,各位大哥,帮帮忙吧 (关于 allocator)

我在运行程序时遇到一个问题,请教各位大哥:
设计一个可变的数组,需要用到默认的分配器,请问如何赋值啊?
部分代码如下:
template<class T, class Allocator = allocator<T> >

class RangeArray {

  T *arrayptr; // pointer to array that underlies the container



  unsigned len;   // holds length of the container

  int upperbound; // lower bound

  int lowerbound; // upper bound



  Allocator a; // allocator
。。。。。。
// Default constructor.

  RangeArray()

  {

    upperbound = lowerbound = 0;

    len = 0;

    arrayptr = a.allocate(0);

  }
。。。。。

书上说这个STL有默认的分配器,可如何给a赋值呢?按照我给的程序这样做的时候,编译老是出错,怎么该啊?
求求各位大哥帮助帮助小弟啊,谢谢了!      

TOP

求求各位大哥了,快点帮我解决这个问题吧,我是急的想跳楼了啊!      

TOP

我看到一段累世的代码:
template<template <typename> class _Alloc>
class ACE_STL_Allocator : public ACE_Allocator
{
  public:
   typedef _Alloc<char> alloc_type;
   
  public:
   /// These methods are defined.
   virtual void *malloc (size_t nbytes);
   virtual void *calloc (size_t nbytes, char initial_value = '\0');
   virtual void *calloc (size_t n_elem, size_t elem_size, char initial_value = '\0');
   virtual void free (void *ptr);
   
   /// These methods are no-ops.
   virtual int remove (void);
   virtual int bind (const char *name, void *pointer, int duplicates = 0);
   virtual int trybind (const char *name, void *&pointer);
   virtual int find (const char *name, void *&pointer);
   virtual int find (const char *name);
   virtual int unbind (const char *name);
   virtual int unbind (const char *name, void *&pointer);
   virtual int sync (ssize_t len = -1, int flags = MS_SYNC);
   virtual int sync (void *addr, size_t len, int flags = MS_SYNC);
   virtual int protect (ssize_t len = -1, int prot = PROT_RDWR);
   virtual int protect (void *addr, size_t len, int prot = PROT_RDWR);
   #if defined (ACE_HAS_MALLOC_STATS)
   virtual void print_stats (void) const;
   #endif /* ACE_HAS_MALLOC_STATS */
   virtual void dump (void) const;
   
  private:
   alloc_type allocator;
};
请问是不是这么用的啊?
我编译的时候为什么说没alloc_type 这个类型呢?      

TOP

http://dev.csdn.net/Develop/article/17/17946.shtm
关于STL不被支持的问题很多,如果出现你说的这种情况也不是很奇怪了。      
上帝说,有问题,找GOOGLE 写程序是很神圣的事情!同样只是装系统,卖菜的大娘会的事情不见得就跟卖菜一样了。

TOP

Allocator a 已经由它的 constructor 初始化好了, 不用赋值, 直接使用即可      
'
◆ 发帖时请【突出主题】, 以便您的问题能够及时得到回复
◆ 发帖时请将您的【代码】或者【脚本】写在 [code] 和 [/code] 中间

TOP

举例:
复制内容到剪贴板
代码:
[color=blue]-(guest@mac:tty1)-(cpp)-
[5653 0] %[/color] cat allocator.cc

#include <memory>

using namespace std;

template <class T, class Allocator = allocator<T> >
class Array
{
public:
    Array(unsigned _len)
    {
        len = _len;
        arrayptr = a.allocate(len);
    }

    ~Array()
    {
        a.deallocate(arrayptr, len);
    }

private:
    Allocator a;
    unsigned len;
    T * arrayptr;
};

int main(int argc, char *argv[])
{
    Array<int> a(10);
    Array<int, allocator<int> > b(10);
}
[color=blue]-(guest@mac:tty1)-(cpp)-
[5653 0] %[/color] g++ allocator.cc
[color=blue]-(guest@mac:tty1)-(cpp)-
[5653 0] %[/color]
      
'
◆ 发帖时请【突出主题】, 以便您的问题能够及时得到回复
◆ 发帖时请将您的【代码】或者【脚本】写在 [code] 和 [/code] 中间

TOP

那怎么做才能使STL被支持呢?      

TOP

难道没哪个C++的版本支持吗?
如果我换个编译的环境,是不是可以解决这个问题呢?      

TOP

什么意思? 现在的编译器一般都支持 STL 啊      
'
◆ 发帖时请【突出主题】, 以便您的问题能够及时得到回复
◆ 发帖时请将您的【代码】或者【脚本】写在 [code] 和 [/code] 中间

TOP

那我想自己做个STL,难道就没办法严整它的正确与否了?      

TOP

 17 12
发新话题