0

Possible Duplicate:
Why should the implementation and the declaration of a template class be in the same header file?

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<Node>;

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 <iostream>
using namespace std;

#include "intarr.h"
class Node;
template <typename t>
class QueueNode;

template <typename t>
class NodeQueue;

Tree:

class Tree{

    Node* root;


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

    Tree(){
        root=NULL;
        level_length=0;
        curr=NULL;
        level=new NodeQueue<Node>;
        level_bak=new NodeQueue<Node>;
    }
// 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 <typename t>
     class QueueNode {
     public:
        QueueNode* next;
        QueueNode* prev;

        t *value;
        QueueNode(){

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

    };

nodequeue:

 template <typename t>
 class NodeQueue {
    QueueNode *head;
    QueueNode *tail;

    //lhs=bottom;

 public:
     NodeQueue(){
        head=NULL;
        tail=NULL;
    }
//....... rest of the code you dont need
Community
  • 1
  • 1
Ahmed-Anas
  • 5,471
  • 9
  • 50
  • 72

2 Answers2

3

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 <typename t>
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.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • yup this did the trick.I simply deleted it since everything was in a single header file. thanks man. but what exactly do you mean by forward declarations? – Ahmed-Anas Oct 11 '12 at 13:26
  • @death_relic0 forward declarations are too large of a topic to be discussed in a comment (I hope you understand). As it's an important language aspect, I suggest you google. – Luchian Grigore Oct 11 '12 at 13:31
1

According to MSDN Compiler Error C2512 is also generated when you are trying to create instance of class that is not complete - forward declared, but full definition is not available.

So you need to include NodeQueue definition header into tree.h.

Rost
  • 8,779
  • 28
  • 50