Initializing objects in an array of pointers to objects

Unable to initialize these non-STL List objects with the following implementation. The Graph class contains an array of pointers to custom List objects. I am sure that I was mistaken in the way I declared my array of lists.

Title Fragment:

class Graph { private: List **lists; int listCount; public: ....... ....... } 

Fragment of implementation:

 //node integer is used for size of array void Graph::setLists(int node) { listCount = node; lists = new List[listCount]; //for is used to initialized each List element in array //The constructor parameter is for List int variable for(int i = 0; i < listCount; i++) lists[i] = new List(i); } 

Errors I get:

 Graph.cpp: In member function 'void Graph::setLists(int)': Graph.cpp:11:28: error: cannot convert 'List*' to 'List**' in assignment 
+4
source share
2 answers

The only problem I see is that you are trying to initialize lists with an array of List objects instead of an array of pointers to List objects.

change

 lists = new List[listCount]; 

to

 lists = new List*[listCount]; 
+4
source

Since you are creating an array of List objects, not an array of List pointers.

 lists = new List*[listCount]; 
0
source

Source: https://habr.com/ru/post/1489361/


All Articles