为什么这个编译器错误会出现在我的C ++项目中 [英] Why is this compiler error coming in my C++ project

查看:148
本文介绍了为什么这个编译器错误会出现在我的C ++项目中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用于定义打印功能的行

错误:在'{'令牌之前不允许使用函数定义



为最后一行我的代码

错误:在输入结束时预期'}'



我尝试过:



  #include   <   iostream  >  

使用 命名空间标准;

struct 节点
{
int 数据;
node * p_right;
node * p_left;
};

node * insert_(node * p_tree, int key)
{
if (p_tree == NULL)
{
node * p_new_tree = new 节点;
p_new_tree-> p_left = NULL;
p_new_tree-> p_right = NULL;
p_tree = p_new_tree;
return p_tree;

}
else if (key< p_tree->数据)
{
p_tree-> p_left = insert_(p_tree-> p_left,key);
return p_tree;
}
else if (key> p_tree-> data)
{
p_tree-> p_right = insert_(p_tree-> p_right,key);
return p_tree;
}

int print(node * p_tree)
{
if (p_tree == NULL)
{
return NULL;
}
else
{
cout<< p_tree->数据;
打印(p_tree-> p_left);
打印(p_tree-> p_right);

}

}

}

int main()
{
node * p_tree = NULL;

insert_(p_tree, 4 );
insert_(p_tree, 5 );
insert_(p_tree, 3 );
insert_(p_tree, 6 );
insert_(p_tree, 2 );
insert_(p_tree, 1 );
insert_(p_tree, 7 );
insert_(p_tree, 8 );

print(p_tree);
}

解决方案

在C和C ++语言中,您无法在另一个函数中定义函数。

你必须在之外移动 print 插入_



缩进可以帮助您阅读代码。正确使用缩进可以防止出现这种问题。

我建议使用程序员的编辑器,如NotePad ++。

Notepad++主页 [ ^ ]

for the line defining print function
error : A function definition is not allowed before '{' token

for the last line of my code
error : expected '}' at the end of the input

What I have tried:

#include <iostream>

using namespace std;

struct node
{
	int data;
	node*p_right;
	node*p_left;
};

node* insert_(node*p_tree,int key)
{
	if(p_tree==NULL)
	{
		node*p_new_tree=new node;
		p_new_tree->p_left=NULL;
		p_new_tree->p_right=NULL;
		p_tree=p_new_tree;
		return p_tree;

	}
	else if(key<p_tree->data)
	{
		p_tree->p_left=insert_(p_tree->p_left,key);
		return p_tree;
	}
	else if(key>p_tree->data)
	{
		p_tree->p_right=insert_(p_tree->p_right,key);
		return p_tree;
	}

	int print(node*p_tree)
	{
		if (p_tree==NULL)
		{
			return NULL;
		}
		else
		{
			cout <<  p_tree->data;
			print(p_tree->p_left);
			print(p_tree->p_right);

		}

	}

}

int main()
{ 
	node*p_tree=NULL;

	insert_(p_tree,4);
	insert_(p_tree,5);
	insert_(p_tree,3);
	insert_(p_tree,6);
	insert_(p_tree,2);
	insert_(p_tree,1);
	insert_(p_tree,7);
	insert_(p_tree,8);

	print(p_tree);
}

解决方案

In C and C++ language, you can't define a function inside another one.
You must move print outside of insert_

Indentation is here to help you to read your code. proper use of indentation prevent this kind of problem.
I recommend the usage of a programmer's editor like NotePad++.
Notepad++ Home[^]


这篇关于为什么这个编译器错误会出现在我的C ++项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆