我试着从类构造函数中调用结构的构造函数,但是它抛出一个错误。我一直在试图解决这个30分钟调用构造函数时出错
结构:
struct node
{
int val;
node* left;
node* right;
node(int x)
{
val = x;
left = NULL;
right = NULL;
}
~node()
{
delete(left);
delete(right);
}
};
类:
class Tree
{
node* head;
node list[1000];
bool flag[1000] = {0};
public:
Tree(int x)
{
head = new node(x);
}
main()方法:
int main()
{
int a[] = {50,35,75,20,45,60,90,10,25,40,47,65,80,120,1,15,23,30,39,
46,49,82,110,21,24,48};
Tree t(a[0]);
我得到的错误是
错误日志:
In constructor 'Tree::Tree(int)':|
|37|error: no matching function for call to 'node::node()'|
|37|note: candidates are:|
|17|note: node::node(int)|
|17|note: candidate expects 1 argument, 0 provided|
|12|note: node::node(const node&)|
|12|note: candidate expects 1 argument, 0 provided|
结构构造函数有一个参数,并在类的构造函数,我用一个参数调用却把错误抛出的程序调用有0个参数。我不知道问题出在哪里。
谢谢。
你需要一个默认的构造函数。 同样的问题[这里] [1] [1]:http://stackoverflow.com/questions/19743115/c-struct-constructor – Matthias