【发布时间】:2012-10-11 13:17:44
【问题描述】:
可能重复:
Why should the implementation and the declaration of a template class be in the same header file?
希望你能帮助我。
我知道这个问题(在进行谷歌搜索后)已被问过数百万次。我确信我的问题的解决方案是在这数百万个问题中的一个,但我找不到它所以我决定问。
我特别收到了这个错误:
错误 1 错误 C2512: 'NodeQueue' : 没有合适的默认构造函数可用 a:\work\fast\semi 5\automata\assignments\progass1\progass1\progass1\tree.h 33 1 progass1
具体行有这样的定义:
level=new NodeQueue<Node>;
下一行也出现同样的错误,但原因是一样的..
我有所有不知道为什么会发生这种情况的默认构造函数。以下是部分代码:
头文件的顶部:
#include <iostream>
using namespace std;
#include "intarr.h"
class Node;
template <typename t>
class QueueNode;
template <typename t>
class NodeQueue;
树:
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{
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(即队列的节点)
template <typename t>
class QueueNode {
public:
QueueNode* next;
QueueNode* prev;
t *value;
QueueNode(){
}
QueueNode(t* value){
next=NULL;
this->value=value;
}
};
节点队列:
template <typename t>
class NodeQueue {
QueueNode *head;
QueueNode *tail;
//lhs=bottom;
public:
NodeQueue(){
head=NULL;
tail=NULL;
}
//....... rest of the code you dont need
【问题讨论】:
-
你所拥有的只是不是一个最小的例子。为了练习起见,试着编造最小的例子来说明你的问题。 (应该是六行左右。)这将帮助您发现错误并在将来提出更好的问题。
-
@kerrek,它们位于单个标头类中。一切都在标题类中。我会尽量减少代码
-
对于初学者,您不需要在
NodeQueue的定义中指定QueueNode<t>吗? -
卢奇安得到了答案:)....
标签: c++ templates visual-c++