#include< iostream> #include< vector> 使用命名空间std; #ifndef MEMORY_H #include< memory> #define MEMORY_H #endif 模板< class T> class TestAllocator; template<> class TestAllocator< void> { public: typedef void * pointer; typedef const void * const_pointer; typedef void value_type; 模板< class U> struct rebind { typedef TestAllocator< U>其他; }; }; 模板< class T> 类TestAllocator { public: //类型定义 typedef T value_type; typedef size_t size_type; typedef ptrdiff_t difference_type; typedef T *指针; typedef const T * const_pointer; typedef T&参考; typedef const T& const_reference; 指针地址(参考值)const {return& value; } const_pointer地址(const_reference value)const {return& value; } //构造函数和析构函数 TestAllocator()throw(){} TestAllocator(const TestAllocator& copy){} 模板< class U> TestAllocator(const TestAllocator< U>&)throw(){} ~TestAllocator()throw(){ } //分配运营商 TestAllocator& operator =(const TestAllocator& rhs){return * this; } 内联指针 allocate(size_type n,const_pointer) = 0) { 指针p; size_type size; size = n * sizeof(T); p =(指针)::操作员新(大小); 返回p; } inline void deallocate(指针p,size_type n = 1) { :: operator delete(p); } void construct(指针p,const T& val) { new((void *)p)T(val); } void destroy(指针p) { ((T *)p) - > ~T(); } size_type max_size()const throw() { return std: :numeric_limits< std :: size_t> :: max()/ sizeof(T); } 模板< class U> struct rebind { typedef TestAllo CATOR< U>其他; }; }; 类TestClass { public: TestClass(int a):b(a){}; ~TestClass(); int getNum(){return b;}; int b; }; // typedef std :: vector< TestClass *> TestClassVector; typedef std :: vector< TestClass *,TestAllocator< TestClass> > TestClassVector; int main(int argc,char * argv []) { TestClassVector a; TestClassVector * tmpVecPtr =& a; TestClass * tmpPtr = new TestClass(5); a.push_back(tmpPtr) ; if((* tmpVecPtr)[0]!= NULL) cout<<" hello main"<< endl; }Hi All,I''m not sure if I''m dealing with a C++ question or a compiler question,so please forgive me if I''m asking in the wrong spot. If so, maybesomeone can direct me to more appropriate spot.In migrating from gcc 3.2.3 to gcc 3.4.3 the following situation comesup.I have the following://typedef std::vector<TestClass* > TestClassVector;typedef std::vector<TestClass*,TestAllocator<TestClass> >TestClassVector;TestClassVector a;TestClassVector * tmpVecPtr = &a;TestClass * tmpPtr = new TestClass(5);a.push_back(tmpPtr);if ( (*tmpVecPtr)[0] != NULL )cout<<"hello main"<<endl;When using the custom allocator this line breaks:(*tmpVecPtr)[0] != NULL )thinking that this field is a TestClass and opposed to the TestClass*which it should be. If I use the first typedef(with no allocatorspecified) it compiles fine with gcc 3.4.3. Also both compile fineunder gcc 3.2.3I''m not clear if there has been a tightening compliance of the compilerand i need to add a method to the allocator class to make this compile,or what exactly my problem is.If anyone can shed some light on this I would be very appreciative.Thanks much!I''ll attach the complete code below:#include <iostream>#include <vector>using namespace std;#ifndef MEMORY_H#include <memory>#define MEMORY_H#endiftemplate <class T>class TestAllocator;template <>class TestAllocator<void>{public:typedef void* pointer;typedef const void* const_pointer;typedef void value_type;template <class U>struct rebind{typedef TestAllocator<U> other;};};template <class T>class TestAllocator{public:// Type definitionstypedef T value_type;typedef size_t size_type;typedef ptrdiff_t difference_type;typedef T* pointer;typedef const T* const_pointer;typedef T& reference;typedef const T& const_reference;pointer address(reference value) const { return &value; }const_pointer address(const_reference value) const { return &value;}// Constructors and destructorTestAllocator() throw() {}TestAllocator(const TestAllocator& copy) {}template <class U>TestAllocator(const TestAllocator<U>&) throw() {}~TestAllocator() throw() {}// Assignment OperatorTestAllocator& operator=(const TestAllocator& rhs) { return *this;}inline pointerallocate(size_type n, const_pointer = 0){pointer p;size_type size;size = n * sizeof(T);p = (pointer) ::operator new(size);return p;}inline voiddeallocate(pointer p, size_type n = 1){::operator delete(p);}voidconstruct(pointer p, const T& val){new((void*) p) T(val);}voiddestroy(pointer p){((T*) p)->~T();}size_typemax_size() const throw(){return std::numeric_limits<std::size_t>::max() / sizeof(T);}template <class U>struct rebind{typedef TestAllocator<U> other;};};class TestClass{public:TestClass(int a): b(a){};~TestClass();int getNum() { return b;};int b;};//typedef std::vector<TestClass* > TestClassVector;typedef std::vector<TestClass*,TestAllocator<TestClass> >TestClassVector;int main(int argc, char *argv[]){TestClassVector a;TestClassVector * tmpVecPtr = &a;TestClass * tmpPtr = new TestClass(5);a.push_back(tmpPtr);if ( (*tmpVecPtr)[0] != NULL )cout<<"hello main"<<endl;}推荐答案 na ********** @ yahoo.com 写道:使用自定义分配器时,此换行符:( * tmpVecPtr)[0]!= NULL) When using the custom allocator this line breaks: (*tmpVecPtr)[0] != NULL ) 错误信息是什么?What is the error message?啊......细节!... :) 在矢量定义之间切换w /或没有 指定的分配器导致它与之不匹配!= NULL。在这种情况下 指定了分配器。 谢谢.. test.cpp:在功能中`int main(int,char **)'': test.cpp:128:错误:''运算符!=''不匹配 '' tmpVecPtr-> std :: vector< _Tp,_Alloc> :: operator [] [with _Tp = TestClass *,_ Alloc = TestAllocator< TestClass>](0ul)!= 0l'' /usr/lib/gcc/x86_64-redhat-linux/3.4.3 /../../../../ include / c ++ / 3.4.3 / bits / vector.tcc: 在成员函数中`void std :: vector< _Tp, _Alloc> :: _ M_insert_aux(__ gnu_cxx :: __ normal_iterato r< typename _Alloc: :指针,std :: vector< _Tp,_Alloc>>,const _Tp&)[with _Tp = TestClass *,_ Alloc = TestAllocator< TestClass>]'':Ahh.. details!... :)Switching between the vector definition either w/ or without theAllocator specified causes it to not match the != NULL. in the casewhere an Allocator is specified.Thanks..test.cpp: In function `int main(int, char**)'':test.cpp:128: error: no match for ''operator!='' in''tmpVecPtr->std::vector<_Tp, _Alloc>::operator[] [with _Tp =TestClass*, _Alloc = TestAllocator<TestClass>](0ul) != 0l''/usr/lib/gcc/x86_64-redhat-linux/3.4.3/../../../../include/c++/3.4.3/bits/vector.tcc:In member function `void std::vector<_Tp,_Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterato r<typename_Alloc::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp =TestClass*, _Alloc = TestAllocator<TestClass>]'': natespama ... @ yahoo.com写道:[email protected] wrote:在矢量定义之间切换w /或没有指定分配器导致它不 匹配!= NULL。在中指定了分配器。 test.cpp:在函数`int main(int,char **)'': test.cpp:128:错误:''运算符!=''在'tmpVecPtr-> std :: vector< _Tp,_Alloc> :: operator [] [与_Tp = TestClass *,_ Alloc = TestAllocator< TestClass>](0ul)!= 0l'' != 0l''???那是什么? /usr/lib/gcc/x86_64-redhat-linux/3.4.3/../../../../include/c++ /3.4.3/bits/vector.tcc: x86_64? 64位? 在成员函数`void std :: vector< _Tp, _Alloc> :: _ M_insert_aux(__ gnu_cxx :: __ normal_iterato r< typename _Alloc :: pointer,std: :vector< _Tp,_Alloc>>,const _Tp&)[with _Tp = TestClass *,_ Alloc = TestAllocator< TestClass>]'': TestClass * tmpPtr = new TestClass(5); 非常值得怀疑。根据C ++标准,''new''抛出异常(并且不会返回 NULL)以防OOM(内存不足)(不是 实际上例如在Linux中)。无论如何... if((* tmpVecPtr)[0]!= NULL) Switching between the vector definition either w/ or without the Allocator specified causes it to not match the != NULL. in the case where an Allocator is specified. test.cpp: In function `int main(int, char**)'': test.cpp:128: error: no match for ''operator!='' in ''tmpVecPtr->std::vector<_Tp, _Alloc>::operator[] [with _Tp = TestClass*, _Alloc = TestAllocator<TestClass>](0ul) != 0l''!= 0l'' ??? What''s that?/usr/lib/gcc/x86_64-redhat-linux/3.4.3/../../../../include/c++/3.4.3/bits/vector.tcc:x86_64? 64Bit? In member function `void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterato r<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = TestClass*, _Alloc = TestAllocator<TestClass>]'': TestClass * tmpPtr = new TestClass(5);Highly questionable. ''new'' throws an exception (and doesn''t returnNULL) in case of OOM (out-of-memory) according to the C++ Standard (notactually e.g. in Linux). Anyway ... if ((*tmpVecPtr)[0] != NULL ) 尝试: if((* tmpVecPtr)[0]!=(TestClass *)0) 重新考虑你的设计!Try:if ((*tmpVecPtr)[0] != (TestClass*) 0)Re-think your design! 这篇关于自定义分配器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-10 22:19