Search code examples
c++compiler-errorsnew-operatordynamic-allocation

Using 'new' keyword with struct in c++


#include "PQueue.h"

struct arcT;

struct coordT {
    double x, y;
};

struct nodeT {
    string name;
    coordT* coordinates;
    PQueue<arcT *> outgoing_arcs;
};

struct arcT {
    nodeT* start, end;
    int weight;
};

int main(){
    nodeT* node = new nodeT; //gives error, there is no constructor
}

My purpose is to create a new nodeT in heap. Error is:

error C2512: 'nodeT' : no appropriate default constructor available


Solution

  • PQueue<arcT *> does not have an appropriate default constructor, so a default constructor for nodeT cannot be generated by the compiler. Either make an appropriate default constructor for PQueue<arcT *> or add a user-defined default constructor for nodeT which constructs outgoing_arcs appropriately.