views:

58

answers:

3

Hello, i have the following code compiled with GCC 4.2 / XCode.

template <typename T>
class irrProcessBufferAllocator
{
public:

    T* allocate(size_t cnt)
    {
        return allocProcessBufferOfType<T>(cnt);
    }

    void deallocate(T* ptr)
    {
        if (ptr)
        {
            releaseProcessBuffer(ptr);
        }
    }

    void construct(T* ptr, const T& e)
    {
        new ((void*)ptr) T(e);//"error: expected type-specifier before 'e' " and
//error: expected `;' before 'e'
    }

    void destruct(T* ptr)
    {
        ptr->~T();//error: expected class-name before ';' token
    }

};

i really can't figure out how to fix the errors. please help,

Thanks.

A: 

How about this?

template <class T> 
class irrProcessBufferAllocator 
{ 
public: 

    T* allocate(size_t cnt) 
    { 
        return allocProcessBufferOfType<T>(cnt); 
    } 

    void deallocate(T* ptr) 
    { 
        if (ptr) 
        { 
            releaseProcessBuffer(ptr); 
        } 
    } 

    void construct(T* ptr, const T& e) 
    { 
        new ((void*)ptr) T(e);//"error: expected type-specifier before 'e' " and 


        //error: expected `;' before 'e' 
    } 

    void destruct(T* ptr) 
    { 
        ptr->~T();//error: expected class-name before ';' token 
    } 
};

int main(){
    irrProcessBufferAllocator<int> i, j;
    int *p = new int;
    i.construct(p, 2);
    i.destruct(p);
}
Chubsdad
I know this might be the problem , but XCode dosen't tell me the type for witch the template is used. Also this is used alot , and i can't remove this allocator out , i just have to find a fix.
Raxvan
I give up, what's the difference in your code versus OP's?
GMan
I don't get it.
Chubsdad
@GMan: in the OP, the template header(template<class T>....) was not there. that was edited later on :) after I posted.
Chubsdad
vs OP? what do you mean by OP?
Raxvan
Original Post/Poster. Not in a good mood, so holding on here only...
Chubsdad
the problem is that xcode doesn't tell me the type T... any way i can find that?
Raxvan
If you look at the error messages and scroll it carefully, you will see the compiler tells you the type 'T' for which the error is detected.
Chubsdad
Xcode doesn't , i just tell the header in which the problem appeared.
Raxvan
@Chubsdad: Ah, gotcha.
GMan
A: 

This just points out the problem , doesn't fix it. i have specialized the template for all POD types by removing the 'new' and '->~T()' from the construct and destruct functions. The errors still appear in the same spot.

Raxvan
Instead of answering here, please edit your post. The posts here are sorted by up-votes, so you can't actually answer someone's post this way. The other way is to comment on that "someone"'s post (:
Kiril Kirov
+2  A: 

Just to make sure, you are not missing necessary includes: <cstddef> for std::size_t and <new> for placement new?

Otherwise those functions would appear to be correct. If that is the entire allocator, it has other flaws, such as missing required typedefs, address() and max_size() methods, as well as a rebind template.


Edit: The only cause for the error could be that you have a function-style macro T defined.

#define T(z) zzz

would make the preprocessor replace all T()'s it encounters, but leave the Ts not followed by brackets.

You could just rename the template argument.

visitor
The stddef is not missing. After including the <new> header the problem still exists.
Raxvan
@Raxvan: Post updated with the probably cause of the error.
visitor
thanks man , this was the problem .... :) , fixed it by changing the T in TYPE
Raxvan