0

In my program I have a list which is a pointer list<COffenceUnit*>* m_attack = nullptr; I initialize this list like that: m_attack = new list<COffenceUnit*>(); in the constructor. At some point in the code I want to send this list to another constructor as a reference. This is the constructor that receives the list:

GameMap::GameMap(const std::list<CDefenceUnit*>* &defenseUnits, const std::list<COffenceUnit*>* &offenseUnits)
{
    mGameMap = new int* [GRID_SIZE];
    for (int i = 0; i < GRID_SIZE; ++i)
        mGameMap[i] = new int[GRID_SIZE];

    updateMap(defenseUnits, offenseUnits);
}

However the compiler throws an error that says:

Error   C2664   'GameMap::GameMap(const GameMap &)': cannot convert argument 1 from
'std::list<CDefenceUnit *,std::allocator<_Ty>> **' to 'const std::list<CDefenceUnit *,std::allocator<_Ty>> *&'

I can't figure out what am I doing wrong here.

2
  • You are confusing yourself with all the use of * and &. You are probably trying to pass it like &m_attack to the constructor when you should use m_attack. Please post a minimal reproducible example. Also why do you have a pointer to a list as a member, and not simply a list? And why does the list have pointers in it and not objects? Using pointers for everything is very bad style in c++. Raw pointer should almost always be avoided. Commented Feb 22, 2020 at 14:19
  • 1
    'GameMap::GameMap(const GameMap &)' is not a part of the code you are showing. Please post a minimal reproducible example. Commented Feb 22, 2020 at 14:27

1 Answer 1

1

You're trying to call the function with a pointer-to-pointer-to-list (std::list<...>**), but the function expects a pointer-to-list (std::list<...>*), so the conversion fails.

Dereference the argument to remove one level of pointer. For example if you had GameMap(defenseUnits), then change that to GameMap(*defenseUnits).


Also you should almost never new std containers, so to initialize your m_attack, it's recommended to do it without new (i.e. without dynamic allocation), like so:

list<COffenceUnit*> m_attack;

Then you also don't need to do m_attack = list<COffenceUnit*>(); later, because the list is already default initialized by the list's constructor.

This also helps you avoid multiple levels of pointers, such as in std::list<...>**.


Also you probably want std::vector instead of std::list, the latter is rarely a better choice.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.