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.

Recommended Answers

All 5 Replies

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.

commented: rashakil fol in hiding +14
commented: always had this funny syntax going wrong, thnx though +2

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.

still the same error...

What's your current code?

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);
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.