开发者

How to create and use list, of type "customClass"

开发者 https://www.devze.com 2023-02-15 14:30 出处:网络
I have created a class \"Node\" to contain a bunch of data.I am trying to make a list of the same type of this class.I am having errors in trying to use push_back() or any of the other functions.

I have created a class "Node" to contain a bunch of data. I am trying to make a list of the same type of this class. I am having errors in trying to use push_back() or any of the other functions.

error is the following:" /home/.../FIFO.cpp|61|error: no matching function for call to ‘std::list >::push_back(Node*&)’|"

Node *tempProcess;
list<Node> processList; //list of all processes

tempProcess = new Node(tempArrivInt, tempExecInt);
processList.push_back(tempProcess);

Can someone 开发者_C百科please help?


The compiler error tells you "Couldn't find a push_back function that takes a Node * as parameter". This is because your list contains Node and not Node *, these are not the same types. Use:

list<Node *>


You create a list of Nodes, but try to insert a Node* (pointer to Node), which is what new returns.

If you want to use a list<Node>, you can write:

processList.push_back(Node(tempArrivInt, tempExecInt));

If you want to create your Node objects with new, you'll need to edit the code in the question so that processList is a list<Node*>. Note that in this case, you will have to manually delete your Node objects before you remove an element from the list, otherwise you'll get a memory leak.

0

精彩评论

暂无评论...
验证码 换一张
取 消