涉及模板的未知错误
Unkown error involving templates
这段代码在一个头文件中,头文件本身可以编译得很好。这个想法是,这是一个BST,我应该创建一个迭代器,按顺序遍历它。
我想我已经很好地理解了这一点,但我一定有错误的函数语法。他们在没有给定类型的情况下进行编译,但当给定int
等类型时,它表示找不到具有int
等的版本。
template <class TKey>
class bst {
private:
struct node {
node() { key=TKey(); link[0]=link[1]=NULL; parent=NULL; }
operator TKey () { return key; }
void print();
TKey key;
node *link[2];
node *parent;
};
public:
class iterator {
public:
private:
friend class bst<TKey>;
node *p;
};
iterator begin();
iterator end();
TKey operator*(iterator rhs){return rhs.p -> key;}
bool operator!=(iterator rhs) {return (!(this.p->key==rhs.p->key));}
void operator++();
bst() { Troot=NULL; }
~bst() { clear(Troot); }
bool test;
bool empty() { return Troot==NULL; }
void clear() { clear(Troot); Troot=NULL; }
node *prev_node;
void erase(TKey &key);
void insert(TKey &key);
void print_inorder() { print_inorder(Troot); }
void print_bylevel();
void print_iterator();
private:
void clear(node *);
node *minmax_key(node *, int);
node *erase(node *, TKey &);
node *insert(node *, TKey &);
void print_inorder(node *);
node *Troot;
};
这就是阶级的定义。
template <class TKey>
class bst<TKey>::iterator bst<TKey>::begin(){
node *temp = Troot;
while(temp -> link[0])
temp = link[0];
cout << temp -> key << endl;
iterator it;
it.p = temp;
return it;
};
template <class TKey>
class bst<TKey>::iterator bst<TKey>::end(){
iterator it;
it.p = NULL;
return it;
};
这些似乎是造成问题的Iterator函数。
bst.h:266:2: error: no match for 'operator!=' in 'it != bst<TKey>::end [with TKey = int]()'
bst.h:266:2: note: candidates are:
并列出了大量候选
然后
bst.h:269:3: error: no match for 'operator++' in '++it'
bst.h: In member function 'bst<TKey>::iterator bst<TKey>::begin() [with TKey = int]':
bst.h:265:22: instantiated from 'void bst<TKey>::print_iterator() [with TKey = int]'
BST_usage1.cpp:33:19: instantiated from here
bst.h:70:3: warning: pointer to a function used in arithmetic [-Wpointer-arith]
bst.h:70:3: error: cannot convert 'int(const char*, const char*)throw ()' to 'bst<int>::node*' in assignment
您对operator!=
的定义在类bst<T>
中,而不是在内部类bst<T>::iterator
中。因此,您定义了一个运算符,它在左手边取bst<T>
,在右手边取bst<T>::iterator
。
如果您在内部iterator
类中移动运算符定义,您应该会发现它工作正常(或者至少会给出更有用的错误消息)。
相关文章:
- 双链表的擦除值函数,未知错误
- 未知C++错误:致命错误:glibc检测到无效的stdio句柄
- 带有unique_ptr的通用单链表,MS visual studio C++中存在未知错误
- 在最坏的情况下试验快速排序.它运行良好,但在最坏的情况下发生未知错误.我想
- C++ [] 重载未知错误
- 打电话时返回未知错误
- C 包括文件会导致未知错误
- Qtcpserver 仅在调试器下在侦听函数上返回未知错误
- std::p romise<void> 抛出未知错误,除非调用睡眠
- 如何在将我的 QT 应用程序交叉编译为 raspberry-pi3 时修复未知错误
- 使用reinterpret_cast的未知错误
- 如何修复QFile打开错误(未知错误),即使文件存在
- 使用c++特征库时出现未知错误
- 使用特征库的 mingw 出现未知错误
- CUDA 未知错误后 cuBLAS 调用
- 未知错误,调试器只给我一个内存地址
- 在子类之外实现函数导致未知错误
- 影响格雷厄姆查找凸包算法的未知错误
- 未知错误(-1000)执行cl_command_ndrange_kernel
- QT NetworkAccessManager未知错误