【发布时间】:2015-01-09 18:22:34
【问题描述】:
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++ 提供的默认功能
【问题讨论】:
-
正如错误所说,您不能将默认参数设置为非静态成员变量。您应该重载 add 以仅获取键并调用具有两个参数的键,并将 root 作为第二个参数。
-
@Borgleader:你应该这样回答。
-
那么,这相当于编写一个包装函数。无论如何我可以不写那个包装函数吗?
标签: c++ class parameters default members