سؤال

I have a class Lobby which handles a list of Players(which is an Abstract Class, only pure virtual methods) now i tried to do the following:

    ...    
    std::list<Player*> list;
    ...
    Lobby::Lobby (std::string name, unsigned int size){
        m_size = size;
        m_name = name;
        std::list<Player*> list(size);
    }
    ...
    void Lobby::removePlayer(int playerID){
    for (std::list<Player*>::iterator it = list.begin(); it != list.end(); it++){
    if ((*it)->id == playerID){
        it = list.erase(it);
    }
}
}
    ...

I always get two erros, and have no glew how to solve them:

error C2259: 'Member' : cannot instantiate abstract
    class

I am almost new in C++, but thought that if i use a list of pointers of the base class it should work?!

  IntelliSense: no suitable user-defined conversion from "std::_List_iterator<std::_List_val<std::_List_simple_types<Member>>>" to "std::_List_iterator<std::_List_val<std::_List_simple_types<Member *>>>" exists**
هل كانت مفيدة؟

المحلول

Few points: first, to your problem. I can see nothing wrong with the code you provided, except thet it is not the code you are compiling. Looks like, you have a list of Members*, and you refer to it as Member. Second, Look at remove_if. it ill do the work for you. Last, looks like you have a memory leak here. you remove instance of player from the list, but do nor release it's memory (calling delete)

نصائح أخرى

I can tell from the error messages that you don't show us the actual code. The second message tells us you are having a list<Member*> and maybe a list<Member>, or at least iterators of the two. A list<Member> could also account for the first error, since the list operations try to construct Member objects which is not possible.

My guess is, that you forgot the * somewhere.

You will also want to look at your objects' lifetimes. C++ has no Garbage Collector, so just erasing the pointers from that list won't kill the Member objects themselves. Better use smart pointers like chris suggests in his comment.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top