class bst
{
private:
typedef struct nod
{
int data;
nod* left;
nod* right;
nod(int key):data(key),left(NULL),right(NULL){}
}node;
node* root;
public:
void create();
void add(int key,node*curr=root);
void c2ll();
void print(){}
该代码无法编译...我得到以下错误。
ain.cpp: In function ‘int main()’:
main.cpp:7:12: error: call to ‘void bst::add(int, bst::node*)’ uses the default argument for parameter 2, which is not yet defined
bt.add(50);
^
In file included from bst.cpp:1:0:
bst.h:14:8: error: invalid use of non-static data member ‘bst::root’
node* root;
^
bst.h:19:28: error: from this location
void add(int key,node*curr=root);
^
bst.h:14:8: error: invalid use of non-static data member ‘bst::root’
node* root;
^
bst.cpp:10:34: error: from this location
void bst::add(int key,node* curr=root)
任何建议都将受到欢迎...我试图避免编写包装器方法,而是使用c ++提供的默认功能
根据C ++标准(8.3.6默认参数)
- ...类似地,即使不进行评估,也不得在默认参数中使用非静态成员,除非它作为类成员访问表达式(5.2.5)的id表达式出现,或者除非使用了它形成指向成员(5.3.1)的指针。[示例:在下面的示例中,X :: mem1()的声明格式错误,因为没有为用作初始化程序的非静态成员X :: a提供对象。
int b;
class X {
int a;
int mem1(int i = a); // error: non-static member a
// used as default argument
int mem2(int i = b); // OK; use X::b
static int b;
};
您可以重载功能add
。例如
void add( int key );
void add( int key, node *curr );
root
默认情况下,第一个功能将使用。它可以简单地调用第二个函数,将节点根作为第二个参数传递。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句