简体   繁体   中英

C++ std::list of pointer to a struct

I believe it's a very easy question but I'm quite confused.

What I am doing wrong ?:

    std::list<curvesdata *> curvelist;
    curvesdata * curve = new curvesdata;

    curvelist.pop_back(curve);

   ---> no matching function for call to 'std::list<curvesdata*>::pop_back(curvesdata*&)

Seems my pointer is converted to a pointer reference ... ? why ?

Thanks.

void pop_back ( );

Does not take any arguments. It removes the last element in the list container, effectively reducing the list size by one. See the reference here .

Since the function does not take any arguments, the compiler tries to tell you the same by giving the error of non matching function.
To know Why the compiler does not report the exact error, see this:

why-the-compiler-does-not-detect-correct-function-signature-in-error


To Add a new element at the end of the list,You should be using:

list::push_back()

To Add a new element at the front of the list, You should be using:

list::push_front()

See the reference here .

std::list::pop_back() doesn't take an argument, it deletes the last element from the list. Looks like you want to add, so use push_back() .

curvelist.push_back(curve);

edit: Now let me wax on about clang++ and its nicer error messages:

#include <list>

int main() {
    std::list<int> x;
    x.pop_back(1);
}


$ clang++ example.cxx 
example.cxx:6:13: error: too many arguments to function call, expected 0, have 1
        x.pop_back(1);
        ~~~~~~~~~~ ^

Just FYI, the error message is telling you exactly what's wrong. There's no member function called pop_back for list that takes an argument.

You may be thinking of push_back : http://www.cplusplus.com/reference/stl/list/push_back/

pop_back() doesn't take an argument. There's no matching call to what you're trying to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM