我在* insert_nodes函数中遇到错误:
错误:在'之前预期的构造函数,析构函数或类型转换('令牌
我在同一个函数中也有问题,我必须重新声明' int节点'作为函数中的参数。但我认为没有必要这样写:
*insert_nodes(start, int nodes)
而不是像这样:
*insert_nodes(start,nodes)
获取getch()的另一个错误。在Netbeans中编译时,它只显示该地方的错误,但它没有提到什么类型的错误。
struct tree_traversal
{
int data;
tree_traversal *left; //left subtree
tree_traversal *right; //right subtree
};
tree_traversal *insert_nodes(tree_traversal *start, int nodes);
void preOrderTraversal(tree_traversal *start);
void postOrderTraversal(tree_traversal *start);
void inOrderTraversal(tree_traversal *start);
int counter = 1;
int main(int argc, char **argv)
{
int choice, nodes;
do
{
switch(choice)
{
case 1:
cout<<"\n\t\a\a Enter the Values:\a\a";
cin>>nodes;
start=insert_nodes(start,nodes);
break;
case 2:
cout<<"\n\t\a\a The Values for In-Order Tree traversal is: \a\a"<<endl;
preOrderTraversal(start);
break;
case 3:
cout<<"\n\t\a\a The Values for In-Order Tree traversal is: a\a"<<endl;
postOrderTraversal(start);
break;
case 4:
cout<<"\n\t\a\a The Values for In-Order Tree traversal is: \a\a"<<endl;
inOrderTraversal(start);
break;
case 5:
exit(0);
}
} while(choice != 5);
return 0;
}
tree_traversal *insert_nodes(tree_traversal *start, int nodes)
{
if(start == NULL)
{
start = new tree_traversal;
start ->left = start ->right = NULL;
start ->data = nodes;
counter++;
}
else if(counter%2 == 0)
start ->left = insert_nodes(start ->left,nodes);
else
start ->right = insert_nodes(start ->right,nodes);
return(start);
}
void preOrderTraversal(tree_traversal *start)
{
if(start != NULL)
{
cout<<start ->data;
preOrderTraversal(start->left);
preOrderTraversal(start->right);
getch();
}
}
void postOrderTraversal(tree_traversal *start)
{
if(start != NULL)
{
postOrderTraversal(start->left);
postOrderTraversal(start->right);
cout<<start ->data;
getch();
}
}
void inOrderTraversal(tree_traversal *start)
{
if(start != NULL)
{
inOrderTraversal(start->left);
cout<<start ->data;
inOrderTraversal(start->right);
getch();
}
}
答案 0 :(得分:4)
*insert_nodes(start, int nodes)
应该是
tree_traversal *insert_nodes(tree_traversal *start, int nodes)
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^
您忘记提及返回类型的数据类型&amp;函数定义中的函数参数。
是的,您需要在函数定义中指定函数参数的数据类型。
答案 1 :(得分:0)
在您的代码中:
*insert_nodes(start, int nodes)
参数start没有类型!