0
struct Node{
    int ID, power, parent;
    vector<int> connectedNodes;

    Node(int ID_arg, int power_arg){
        ID = ID_arg;
        power = power_arg;
        parent = -1;
    }
};

struct Graph{
    int rootID;
    map<int, Node> nodes;
    map<int,int> subtreeSizes;

    Graph(){
        rootID = 1;
        nodes[1] = new Node(1,0);
    }

};

I must be having a serious lapse right now because I have no idea what's wrong. It isn't liking the way I am putting the node in the node map.

1 Answer 1

3

That's because you have a type mismatch, which, if you posted the compile error, would be obvious:

nodes[1] = new Node(1,0);
^^^^^^^^   ^^^^^^^^^^^^^
  Node&       Node*

You probably want to do something like:

nodes[1] = Node(1, 0);

That won't work in this particular case since Node isn't default-constructible and map::operator[] requires this. The following alternatives will work regardless:

nodes.insert(std::make_pair(1, Node(1, 0));
nodes.emplace(std::piecewise_construct,
              std::forward_as_tuple(1),
              std::forward_as_tuple(1, 0));
// etc.
Sign up to request clarification or add additional context in comments.

7 Comments

Your code is trying to store a newly allocated pointer to a Node in a map of Nodes (not pointers to nodes). Barry's suggestion fixes your problem. If that's not your problem, be more explicit in your question. No mind readers here.
I must be misunderstanding something then because that looks like it is needlessly complex for what I am trying to do.
I believe Barry is just citing 3 possible solutions to get your code to compile
I had already tried "nodes[1] = Node(1,0);" and that gave me an error as well (which is why I tried "new")
"candidate expects 2 arguments, 0 provided, Node::Node(const Node&)" for the "struct Node{" line
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.