| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#11
| |||
| |||
| PeterAPIIT@gmail.com wrote: [...] > template <class T> > class MyAllocator > { > public: > > // Type Definitions > > typedef T value_type; [...more types...] > > // Member Function > > MyAllocator(){} > > template <class U> > MyAllocator(const MyAllocator<T> &rhs){} ^^^ The template parameter U is not used. Do you mean this instead? template <class U> MyAllocator(const MyAllocator<U> &rhs){} [...] > // ============== Global Functions =============== > > // Non member function cannot declare as constant You cannot declare the functions as constant, but the parameter: > template<class T1, class T2> > bool operator==(MyAllocator<T1>& first, MyAllocator<T2>& second) bool operator==(const MyAllocator<T1>& first, const MyAllocator<T2>& second) > { > return first == second; What function should be called here? The function will call itself. This is infinite recursion. > } > > template<class T1, class T2> > bool operator!=(MyAllocator<T1>& first, MyAllocator<T2>& second) > { > return first!=second; > } Same as above. -- Thomas |
|
#12
| |||
| |||
| PeterAPIIT@gmail.com wrote: > Please help me. We can't help if we don't know your problem. If your code doesn't compile, write what error messages you get, and on which line of code the error is. If your code compiles but doesn't work, tell us what you expect and what did you get instead. Also see here: http://www.parashift.com/c++-faq-lit...t.html#faq-5.8 -- Thomas |
|
#13
| |||
| |||
| Why cannot use "using" in header file ? how do resolve the operator == and operator!= ? Thanks for your help. |
|
#14
| |||
| |||
| Another error is Quote:
Code: void deallocate(pointer aPtr, size_type allocateSize)
{
if (aPtr != 0)
{
destroy(aPtr);
::operator delete aPtr[allocateSize];
}
}
This is my complete code: Code: template <class T>
class MyAllocator
{
public:
// Type Definitions
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef size_t size_type; // Unsigned
// Signed
typedef ptrdiff_t difference_type;
// Member Function
MyAllocator(){}
template <class U>
MyAllocator(const MyAllocator<U> &rhs){}
~MyAllocator(){}
// MyAllocator<T>::rebind<U>::other;
template <class U>
struct rebind
{
typedef MyAllocator<U> other;
};
pointer address(reference memory) const
{
return &memory;
}
const_pointer address(const_reference memory) const
{
return &memory;
}
size_type max_size() const
{
return size_t(+99);
}
pointer allocate(size_type allocateSize)
{
return static_cast<pointer> (::operator new (allocateSize));
}
void deallocate(pointer aPtr, size_type allocateSize)
{
if (aPtr != 0)
{
destroy(aPtr);
::operator delete aPtr[allocateSize];
}
}
void construct(pointer aPtr, const_reference value)
{
if (aPtr != 0)
{
// ((void *)aPtr) is placement new
// T(value) convert to T type
::operator new ((void *)aPtr) T(value);
}
}
void destroy(pointer aPtr)
{
if (aPtr != 0)
{
aPtr->~MyAllocator<T>;
}
}
};
/*
No refernce type to void* -That's why need
specialization for void.
*/
// ============== Global Functions ===============
// Non member function cannot declare as constant
template<class T1, class T2>
bool operator==(MyAllocator<T1>& first, MyAllocator<T2>& second)
{
return first == second;
}
template<class T1, class T2>
bool operator!=(MyAllocator<T1>& first, MyAllocator<T2>& second)
{
return first!=second;
}
Please correct me if i wrong.
A billion thanks for your help.
|
|
#15
| |||
| |||
| On 2008-09-07 11:27, PeterAPIIT@gmail.com wrote: > Why cannot use "using" in header file ? Since you have not quoted the part you are replying to I can only guess you meant "why can I not use "using namespace" in a header file?" and the answer is that you can, but it is bad. The reason is simple, every file that included your header will get the effects of the "using namespace", consider the following: ------ Test.h ----- #ifndef TEST_H #define TEST_H #include <iostream> using namespace std; void foo() { std::cout << "foo()\n"; } #endif ------ Test.cpp ----- #include <vector> #include "Test.h" template<typename T> class vector { T t; }; int main() { vector<int> v; // Error } The problem is that the "using namespace std;" in Test.h" is in effect in Test.cpp, and vector<int> can now refer to your class or std::vector. In some cases you might get even worse problems because you do not get an ambiguity and instead you use some type/function which you did not intend to use, and the only clue is that the program does not work as it should. Therefore you should avoid "using namespace" in headers. -- Erik Wikström |
|
#16
| |||
| |||
| Quote:
Are you trying to explain that name clashed between user defined code and standard namespace ? After listen to you advise, i guess we can include any header file in our own header file but cannot using whole namespace instead explicit specify which function or class to use. In the example you given, compiler cannot resolve vector defined by us ans std. Therefore, an ambiguity has occurred. Am i correct ? Another question is how about code at below ? Code: pointer allocate(size_type allocateSize)
{
return static_cast<pointer> (::operator new (allocateSize));
}
void deallocate(pointer aPtr, size_type allocateSize)
{
if (aPtr != 0)
{
destroy(aPtr);
::operator delete aPtr[allocateSize];
}
}
void construct(pointer aPtr, const_reference value)
{
if (aPtr != 0)
{
// ((void *)aPtr) is placement new
// T(value) convert to T type
::operator new ((void *)aPtr) T(value);
}
}
void destroy(pointer aPtr)
{
if (aPtr != 0)
{
aPtr->~MyAllocator<T>;
}
}
template<class T1, class T2>
bool operator==(const MyAllocator<T1>& first,
const MyAllocator<T2>& second)
{
return true;
}
template<class T1, class T2>
bool operator!=(const MyAllocator<T1>& first,
const MyAllocator<T2>& second)
{
return false;
}
What code is need to add ? A billion thanks for your help. |
|
#17
| |||
| |||
| Yet another question is when i try to compile my code. It shown my error : Code: void destroy(pointer aPtr)
{
if (aPtr != 0)
{
aPtr->~MyAllocator<T>;
}
}
error C2325: 'MyAllocator<T>' : unexpected type to the right of '->~':
expected 'std::_List_nod<_Ty,_Alloc>::_Node '
void construct(pointer aPtr, const_reference value)
{
if (aPtr != 0)
{
// ((void *)aPtr) is placement new
// T(value) convert to T type
::operator new ((void *)aPtr) T(value);
}
}
error C2665: 'operator new' : none of the 5 overloads could convert
all the argument types
error C2146: syntax error : missing ';' before identifier 'T'
void deallocate(pointer aPtr, size_type allocateSize)
{
if (aPtr != 0)
{
destroy(aPtr);
::operator delete aPtr[allocateSize];
}
}
error C2146: syntax error : missing ';' before identifier 'aPtr'
warning C4551: function call missing argument list
A billion thanks for your help. |
|
#18
| |||
| |||
| This is my intention of my code Code: void deallocate(pointer aPtr, size_type allocateSize)
{
if (aPtr != 0)
{
destroy(aPtr);
// ::operator delete aPtr[allocateSize];
// I want delete the memory of container
}
}
void construct(pointer aPtr, const_reference value)
{
if (aPtr != 0)
{
// ((void *)aPtr) is placement new
// T(value) convert to T type
::operator new ((void *)aPtr) T(value);
// I want to do placement new to initialize the memory which has
allocated
}
}
void destroy(pointer aPtr)
{
if (aPtr != 0)
{
aPtr->~MyAllocator<T>;
// I want to do uninitialize the container value hold.
}
}
A billion thanks for your help. |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.