我是一个c++初学者,我写了下面的代码,然后运行时怎么会出现错误

来源:百度知道 编辑:UC知道 时间:2024/09/28 07:13:36
error 1. 'f' : undeclared identifier; 2.'main' : function should return a value; 'void' return type assumed 3.'f' : redefinition; different type modifiers下面是我写的,到底是哪里错了?

#include "stdafx.h"
main()
{ int i=2,p;
p=f(i,++i);
printf("%d",p);
}
int f(int a, int b)
{ int c;
if(a>b) c=1;
else if(a==b) c=0;
else c=-1;
return(c);
}

函数必须先声明后调用。你在主函数中没有有声明函数int f(int a, int b) 就直接调用会出错。

c++ main函数是必须要声明返回值的,而且标准c++应该返回int类型。而且函数要先声于调用声明或者定义。