我想在模板BSTree.h中创建两个重载操作符,并且遇到了错误,它们确实不会告诉我问题是什么。单独或联合运行错误代码的搜索没有为我带来任何东西。无法声明2个朋友重载<<在模板中.h
第一个重载< <为BSTree不会导致编译任何错误,但第二次超载< <我为我的节点结构建立保持返回以下错误:
错误C4430:缺少类型说明符 - 假设为int。注意:C++不支持默认int
错误C2143:语法错误:缺少“”前‘*’
#ifndef BSTREE_H
#define BSTREE_H
#include <iostream>
#include <fstream>
template <typename T>
class BSTree{
friend ostream& operator<<(ostream&, const BSTree<T>&);
public:
BSTree();
//BSTree(const BSTree &);
~BSTree();
void buildTree(ifstream&);
void setType(char);
bool getType(char);
bool insert(T*);
bool isEmpty();
private:
char type;
struct Node{
T* data;
//subnode[0] == left subtree
//subnode[1] == right subtree
Node* subnode[2];
};
Node* head;
void destructorHelper(Node* &);
bool insertHelper(T*, Node* &);
friend ostream& operator<<(ostream&, const Node*&);
};
编译器说,发生在这里的节点超载< <代码行中的错误。
template <typename T>
ostream& operator<<(ostream &output, const BSTree<T> &out) {
if(head != NULL)
output << head;
return output;
}
template <typename T>
ostream& operator<<(ostream &output, const Node* &out) {
if(out != NULL){
output << out->subnode[0];
output << *out->data;
output << out->subnode[1];
}
return output;
}
不允许我宣布2重载< <在同.H即使它们是不同的对象?或者我在我的代码中搞了些什么?
这部分的工作,这样做只是常量BSTree ::节点*返回C4346错误。我不得不做const const typename :: BSTree ::节点*出来让错误消失 –
Moniker
2010-12-07 19:28:08