0
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.

3
Contributors
5
Replies
28
Views
3 Years
Discussion Span
Last Post by mike_2000_17
2

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.

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

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.

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

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.