I am trying to make a nodelist and I have two classes: Node & Nodelist. The constructor of Node looks like this:
Node::Node(int identifier, bool weighted){
ID_ = identifier;
numberOfConnections_ = 0;
weighted_ = weighted;
}
When I try to add a connection between the nodes with this method:
void Nodelist::addOneWayConnection(int source, int target){
connections_[source] = &Node(source, weightedlist_); <-- error
connections_[target] = &Node(source, weightedlist_); <-- error
connections_[source]->addConnection(connections_[target]);
}
I get the error:
error: taking the address of a temporary object of type 'Node'
How do I store a reference to the class Node?
Thanks in advance!
You don't want to store a reference to that. It ceases to exist after the expression. It's "temporary," as the error is telling you. You should either store Nodes directly, or store a pointer and use new
to allocate them.
Edit: from your comment I've noticed that connections_ actually expects a pointer (as I said you could use above). You need to allocate new nodes.
connections_[source] = new Node(source, weightedlist_);
connections_[target] = new Node(source, weightedlist_);