PDA

View Full Version : C++ compile errors



newport_j
December 13th, 2010, 07:34 PM
*************network:~/Desktop/WEGtest$

*************network:~/Desktop/WEGtest$ gcc -S fmod.c

*************network:~/Desktop/WEGtest$ g++ -S fmod.c

fmod.c:60: error: ‘x’ was not declared in this scope

fmod.c:60: error: ‘y’ was not declared in this scope

fmod.c:60: error: initializer expression list treated as compound expression

fmod.c:62: error: expected ‘,’ or ‘;’ before ‘double’

fmod.c:63: error: expected unqualified-id before ‘{’ token

*************network:~/Desktop/WEGtest$

I get the above output when I try to compile fmod.c in g++. it compiles perfectly when i compile it with gcc.

I am not sure what the errors mean.


The code is




/*
* Copyright (c) 1989 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that: (1) source distributions retain this entire copyright
* notice and comment, and (2) distributions including binaries display
* the following acknowledgement: ``This product includes software
* developed by the University of California, Berkeley and its contributors''
* in the documentation or other materials provided with the distribution
* and in all advertising materials mentioning features or use of this
* software. Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#ifndef lint
static char sccsid[] = "@(#)fmod.c 5.2 (Berkeley) 6/1/90";
#endif /* not lint */
/* fmod.c
*
* SYNOPSIS
*
* #include <math.h>
* double fmod(double x, double y)
*
* DESCRIPTION
*
* The fmod function computes the floating-point remainder of x/y.
*
* RETURNS
*
* The fmod function returns the value x-i*y, for some integer i
* such that, if y is nonzero, the result has the same sign as x and
* magnitude less than the magnitude of y.
*
* On a VAX or CCI,
*
* fmod(x,0) traps/faults on floating-point divided-by-zero.
*
* On IEEE-754 conforming machines with "isnan()" primitive,
*
* fmod(x,0), fmod(INF,y) are invalid operations and NaN is returned.
*
*/
#if !defined(vax) && !defined(tahoe)
extern int isnan(),finite();
#endif /* !defined(vax) && !defined(tahoe) */
extern double frexp(),ldexp(),fabs();
#ifdef TEST_FMOD
static double
_fmod(x,y)
#else /* TEST_FMOD */
double
fmod(x,y)
#endif /* TEST_FMOD */
double x,y;
{
int ir,iy;
double r,w;
if (y == (double)0
#if !defined(vax) && !defined(tahoe) /* per "fmod" manual entry, SunOS 4.0 */
|| isnan(y) || !finite(x)
#endif /* !defined(vax) && !defined(tahoe) */
)
return (x*y)/(x*y);
r = fabs(x);
y = fabs(y);
(void)frexp(y,&iy);
while (r >= y) {
(void)frexp(r,&ir);
w = ldexp(y,ir-iy);
r -= w <= r ? w : w*(double)0.5;
}
return x >= (double)0 ? r : -r;
}
#ifdef TEST_FMOD
extern long random();
extern double fmod();
#define NTEST 10000
#define NCASES 3
static int nfail = 0;
static void
doit(x,y)
double x,y;
{
double ro = fmod(x,y),rn = _fmod(x,y);
if (ro != rn) {
(void)printf(" x = 0x%08.8x %08.8x (%24.16e)\n",x,x);
(void)printf(" y = 0x%08.8x %08.8x (%24.16e)\n",y,y);
(void)printf(" fmod = 0x%08.8x %08.8x (%24.16e)\n",ro,ro);
(void)printf("_fmod = 0x%08.8x %08.8x (%24.16e)\n",rn,rn);
(void)printf("\n");
}
}
main()
{
register int i,cases;
double x,y;
srandom(12345);
for (i = 0; i < NTEST; i++) {
x = (double)random();
y = (double)random();
for (cases = 0; cases < NCASES; cases++) {
switch (cases) {
case 0:
break;
case 1:
y = (double)1/y; break;
case 2:
x = (double)1/x; break;
default:
abort(); break;
}
doit(x,y);
doit(x,-y);
doit(-x,y);
doit(-x,-y);
}
}
if (nfail)
(void)printf("Number of failures: %d (out of a total of %d)\n",
nfail,NTEST*NCASES*4);
else
(void)printf("No discrepancies were found\n");
exit(0);
}
#endif /* TEST_FMOD */



Any help appreciated. Thanx in adavance.

Newport_j

dwhitney67
December 13th, 2010, 07:48 PM
A function declaration such as the following is ok for C (but rarely used by anyone anymore), but AFAIK it was never accepted for the C++ standard.


void function(x,y)
int x, y;
{
...
}

The correct syntax for C++ would be:


void function(int x, int y)
{
...
}


One has to question why you are attempting to compile C code using the "g++" compiler? Did someone indicate to you that C and C++ were the same? If they did, they were foolish to state that.

talonmies
December 13th, 2010, 07:49 PM
It contains a K&R style function declaration which is illegal in C++.

Of course it begs the question why you are compiling an evil old BSD era fmod function when there is an implementation in the C++ standard library....

newport_j
December 13th, 2010, 08:52 PM
It was not my choice to do this. I was given this legacy code to work with. It is incredibly complicated from a mathematical point of view.

As they say years in the making.

It was written by engineers and mathematicians, not programmers, and it was written in c: the portable language.

The analytical tools that i am using are writen for both c andf c++, but c is being phased out.

So I think it wise to move it to c++.

That is all. We at this government facility have a lot of legacy code. The code is written in very outdated languages.

It would nice to have training in newer languages, but that is not about to happen with current budget shortfalls.


It does seem that popular literature still talks about c and c++ as if they are closely related.

Newport_j


Newport_j

talonmies
December 13th, 2010, 09:53 PM
It does seem that popular literature still talks about c and c++ as if they are closely related.
They are closely related. But they are not the same. And over time they have diverged considerably, especially in areas that were not in C at the time that C++ was developed and the Stroustrup book was published in the mid-eighties.

newport_j
December 13th, 2010, 10:47 PM
I think I understand. I have to ask one question, though. The complex type in c was introduced in c99 to satisfy FORTRAN programmers transitioning to c.

Could someone elaborate on that? Everywhere I read about c99 and complex type I read this piece of info. What exactly happened here?


Newport_j

MadCow108
December 13th, 2010, 10:56 PM
fortran had COMPLEX, a complex number type needed often for e.g. scientific purposes.
C89 did not have this type.
So to help transition of fortran coders to C, C99 standardized a complex type for C too.
http://www.gnu.org/s/libc/manual/html_node/Complex-Numbers.html

C++ also has a complex number type (in form of a class with overloaded operators):
http://www.cplusplus.com/reference/std/complex/complex/

Arndt
December 14th, 2010, 12:05 AM
It was not my choice to do this. I was given this legacy code to work with. It is incredibly complicated from a mathematical point of view.

As they say years in the making.

It was written by engineers and mathematicians, not programmers, and it was written in c: the portable language.

The analytical tools that i am using are writen for both c andf c++, but c is being phased out.

So I think it wise to move it to c++.

That is all. We at this government facility have a lot of legacy code. The code is written in very outdated languages.

It would nice to have training in newer languages, but that is not about to happen with current budget shortfalls.


It does seem that popular literature still talks about c and c++ as if they are closely related.

Newport_j


Newport_j

If you have been given a large amount of legacy code written in C, you cannot phase out C. You may want to carefully adapt it to more modern C standards, in order to benefit from warnings and analysis tools. You have to decide yourself whether that is a good thing to do.

dwhitney67
December 14th, 2010, 12:19 AM
It was not my choice to do this. I was given this legacy code to work with. It is incredibly complicated from a mathematical point of view.

As they say years in the making.

It was written by engineers and mathematicians, not programmers, and it was written in c: the portable language.

The analytical tools that i am using are writen for both c andf c++, but c is being phased out.

So I think it wise to move it to c++.

That is all. We at this government facility have a lot of legacy code. The code is written in very outdated languages.

It would nice to have training in newer languages, but that is not about to happen with current budget shortfalls.


It does seem that popular literature still talks about c and c++ as if they are closely related.

Newport_j


Newport_j

I work on a gov't project also, and my task, along with 4 others, is to port C++, C, Java, csh, and perl, from a legacy system (Solaris 1.2) to RHEL 5.8. All of us on the team are competent, and suited for the task.

You chided the original developers of your legacy code (which under certain circumstances is fine), but frankly you seem no better than they with your lack of experience. Regardless, rather than whine about the hurdles you face, why don't you fix (ie port) the code versus just trying to fit a "square" into a "circle".

It may take you longer, and you may have battles with mgmt trying to define what term "porting" implies. In your spare time, spend time going through a C++ book. Write yourself little sample programs so that you become more comfortable with the C++ STL (http://www.cplusplus.com/reference/stl/).

Nobody gains "useful" experience by merely taking a C++ course; experience comes from programming, day-in/day-out, and learning from mistakes that you may make along your journey.

When you compile a C (or C++) module, and you encounter an error, the first thing is to go to the line where the compiler is complaining. Examine the error, learn from it, and fix it. Don't hesitate. It doesn't matter that you find the perfect fix; it's gov't code that will probably not be used within 5 years. And if you have the attitude that the gov't gets what it pays for, then you should not sweat a tear worrying about the failure of the program.

I used to work for the gov't, and now I work as a contractor (for the same agency). I see many dumb decisions being made, many days in which resources (ie. people) are under-utilized, and where innovation is stifled because of some inherent fear to explore the unknown. It makes me want to blow off my job, but damn it, I like the money. So instead I just sit back, do the minimum required, and just shut my mouth.

talonmies
December 14th, 2010, 08:34 AM
The analytical tools that i am using are writen for both c andf c++, but c is being phased out.

So I think it wise to move it to c++.

Isn't actually the case that this whole exercise is being driven solely by the desire to compile this code with Intel's C++ compiler which supports Cilk Plus (http://software.intel.com/en-us/articles/intel-cilk-plus/)? With the final objective of using that products compiler driven parallel programming primitives for performance improvement reasons?

newport_j
December 14th, 2010, 05:06 PM
I am not chiding anybody for anything. The economics of this business requires an improvement in the code's execution speed. They gave it to me to check the math for improvments. I found none, all that could be done had been done. Then they said well why don't you try any other method you can think of and that is what I am doing. I get this assignment when work on other projects slows down.

Many have tried, but no one has had any success.

I would rather analyze it all in c and be done with it. I have taken courses in c++, but never programmed in it.

As a programmer I am a pretty good mathematican. If I can find what I need in c, I will. If it requires c++, then I will try to get it to c++ compile.

If there is a c++ equivalent to fmod.c , please give me the link.

If you want an idea of the age of the code well it was originally in FORTRAN.

Newport_j

Zugzwang
December 14th, 2010, 06:20 PM
If there is a c++ equivalent to fmod.c , please give me the link.


Huh? There's even a built-in function for "fmod" in the GNU compiler collection - just include "math.h". It will probably use the built-in function of your processor for this purpose. In any case, it will be better than the one you provided - both in terms of numerical stability and speed.

newport_j
December 14th, 2010, 07:10 PM
Thanks for the advice. I will use it.

Newport_j