我試着從類構造函數中調用結構的構造函數,但是它拋出一個錯誤。我一直在試圖解決這個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