VC6.0 error C2143: syntax error : missing ')' before '&'

来源:百度知道 编辑:UC知道 时间:2024/05/15 18:04:06
我在编译时出现:
error C2143: syntax error : missing ')' before '&'
error C2143: syntax error : missing '{' before '&'
这两个错误,到底错在哪里啊,我半天找不出来郁闷
以下是原代码:
#include <stdio.h>
#include <math.h>
int root(float a,float b,float c,float &x1,float &x2)
{ float d; int sw;
d=b*b-4*a*c;
if(d<0)
{
x1=-b/2/a;
x2=sqrt(-d)/2/a;
sw=0;
}
else
{
x1=(-b+sqrt(d))/2/a;
x2=(-b-sqrt(d))/2/a;
sw=1;
}
return sw;
}
void main()
{ float x,y;
if(root(13.4,5.37,-4.1,x,y))
printf("x1=%f x2=%f\n",x,y);
else
printf("x1=%f+%fi x2=%f-%fi\n",x,y,x,y);
}
我现在是在6.0C++上写的.c程序啊,
回二楼,我在定义一个函数再调用它啊
回三楼,我的是有错误啊,怎么回事

我后来把文件名改成了.cpp,编译就没有错误了,用.c就有错误,到底是怎么回事呢,谁可以回答下,谢谢

回 joodo, C里面也有加&的啊,是叫函数的引用调用啊
你说C应该用指针?请问下怎么改?还是不明白

你那个“float &x1,float &x2”是c++用法吧?c应该用指针啊

加&是c++特有的叫引用啊,放到C里……

C语言里没有引用啊,引用是C++特有的!C里加&只是指针的地址。应指针改的方法是

int root(float a,float b,float c,float *x1,float *x2)
{
... /*省略,只要将对x1、x2的操作改为对*x1、*x2的操作即可*/
}

void main()
{
float x, y;
if(root(13.4,5.37,-4.1,&x,&y))
printf("x1=%f x2=%f\n",x,y);
else
printf("x1=%f+%fi x2=%f-%fi\n",x,y,x,y);
}

没错误啊?只有warning。VC6.0
--------------------Configuration: paixu - Win32 Debug--------------------

Compiling...
hehe.cpp
c:\program files\microsoft visual studio\myprojects\for_fun\hehe.cpp(9) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
c:\program files\microsoft visual studio\myprojects\for_fun\hehe.cpp(14) : warning C4244: '=' : conversion from 'double' to 'float