void StrVec::free()
{
    if (elements) {
        for_each(elements, first_free, 
            [this] (std::string* p) { alloc.destroy(p); });
        alloc.deallocate(elements, cap - elements);
    }
}

produced this error: Cruel Error :(

it was the second compiling error, if I removed the pointer, it will produced the first one. which is kind of -.-"
so, I tried with const pointer, const reference etc. and nothing works, yay! please help.

Is elements a collection of string pointers or of strings? Judging from the error messages I'd say it's the latter, so your lambda needs to take a string or string reference as its argument.

Since destroy takes a pointer, you'll have to pass a pointer to your string to it. Note that if you make your lambda take a string, it will have its own unique address that's distinct from that of the string in the collection, so you probably want to use a reference.

Comments
always had this funny syntax going wrong, thnx though
rashakil fol in hiding

still the same error...

class StrVec{
private:
    std::allocator<std::string> alloc;

    std::string* elements;
    std::string* first_free;
    std::string* cap;
};

this is some of the private members in the class. where elements point to the beginning of element, first_free point to past-of-ending element and cap is pointing to ending of allocated memory block.

void StrVec::free()
{
    if (elements) {
        for_each(elements, first_free, 
            [this] (std::string& p) { alloc.destroy(p); });
        alloc.deallocate(elements, cap - elements);
    }
}

The correct code is this:

void StrVec::free()
{
    if (elements) {
        for_each(elements, first_free, 
            [this] (std::string& p) { alloc.destroy(&p); });
        alloc.deallocate(elements, cap - elements);
    }
}
This article has been dead for over six months. Start a new discussion instead.