1
#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

iammilind
  • 68,093
  • 33
  • 169
  • 336
SegFault
  • 1,024
  • 2
  • 16
  • 25
  • 5
    There's nothing wrong with that code - `coordT` has an implicitly-generated constructor, which does nothing. Do you really get an error from that code? What exactly does it say? – Mike Seymour Oct 14 '11 at 12:07
  • error C2512: 'nodeT' : no appropriate default constructor available – SegFault Oct 14 '11 at 12:08
  • 1
    You have a different problem. The code you are providing compiles fine (Have you declared any constructor?) You can try pasting it to ideone or any other similar page to verify this. – David Rodríguez - dribeas Oct 14 '11 at 12:08
  • Are you sure you don't define a parameterized constructor in coordT? – Luc Touraille Oct 14 '11 at 12:09
  • 3
    @SegFault: there is no `nodeT` in the code you posted, so whatever you're really compiling, it has some error that this code doesn't. – Steve Jessop Oct 14 '11 at 12:09
  • Are you sure that's your complete code? If you've given your class a constructor with arguments, then that will suppress the implicit default constructor; you will get that error then. And what's a `nodeT`? – Mike Seymour Oct 14 '11 at 12:10
  • @Mike Seymour, I though I could simplify the question, but now I guess problem is not what I though, because coordT* coord = new coordT works fine but nodeT does not if used in same way – SegFault Oct 14 '11 at 12:19

3 Answers3

5

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.

Dark Falcon
  • 43,592
  • 5
  • 83
  • 98
4

If the currently posted code in the question is an exact copy, then the only possible cause for this error is that PQueue<…> doesn’t define a default constructor, and defines another constructor instead.

Otherwise this code would compile.

More precisely, since you didn’t define a constructor for your structures, C++ tries to auto-generate them. It can only do this, though, as long as all its member variables are appropriately default constructible or initialisable. std::string has a default constructor, and coordT*, being a pointer, can be initialised. So only PQueue<…> remains as the culprit.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
3

THis may not be your problem but you've only declared one pointer on this line in arcT :-

nodeT* start, end;

You've declared start as a pointer and end as an actual nodeT object. Is this what you wanted to do?

jcoder
  • 29,554
  • 19
  • 87
  • 130
  • No, this is not what I wanted to do, I wanted both of them to be pointer, you saved me a lot of time – SegFault Oct 14 '11 at 12:50