Sunday, 19 March 2017

c++ template no appropriate default constructor











was hoping you could help me out.



I know this question (after doing a google search) has been asked millions of times. I'm sure the solution to my problem is in one of those millions of asked questions, but I couldnt find it so I decided to ask.




I am getting this error specifically:




Error 1 error C2512: 'NodeQueue' : no appropriate default constructor available a:\work\fast\semi 5\automata\assignments\progass1\progass1\progass1\tree.h 33 1 progass1




the specific line has this defination:



level=new NodeQueue;



getting the same error for the next line as well but the cause is the same..



I've got default contructors for everything not sure why this is happening.. Here are parts of the code:



the top part of the header file:



#include 
using namespace std;


#include "intarr.h"
class Node;
template
class QueueNode;

template
class NodeQueue;


Tree:




class Tree{

Node* root;


int level_length;
Node* curr;
NodeQueue * level,*level_bak;
public:


Tree(){
root=NULL;
level_length=0;
curr=NULL;
level=new NodeQueue;
level_bak=new NodeQueue;
}
// I doubt you need the rest...



class node



class Node{
public:
Node *top,*right,*bottom,*left,*prev;
Node *a,*b,*c;
int row,col;
Node(){


}
Node(int x,int y){
top=right=bottom=left=prev=NULL;
row=x;col=y;
a=b=c=NULL;
}

};



queuenode (i.e queue's node)



 template 
class QueueNode {
public:
QueueNode* next;
QueueNode* prev;

t *value;
QueueNode(){


}
QueueNode(t* value){
next=NULL;
this->value=value;
}

};



nodequeue:



 template 
class NodeQueue {
QueueNode *head;
QueueNode *tail;

//lhs=bottom;

public:

NodeQueue(){
head=NULL;
tail=NULL;
}
//....... rest of the code you dont need

Answer



Since this is a compiler error (not linker, as most template error questions), I'm guessing it's because you forward-declare the type:



template 

class NodeQueue;


why do you forward-declare it instead of including the file? To construct an object you need the full definition, so #include "NodeQueue.h".



Use forward declarations where a complete type isn't needed.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...