[net.lang.c++] C++ on existing C programs.

stephen@comp.lancs.ac.uk (Stephen J. Muir) (08/14/86)

C++ is supposed to be upwards compatible with C.  Can anybody, therefore,
explain why the following does not compile (under 4.2):

# include <sys/param.h>
# include <sys/time.h>
# include <sys/file.h>
# include <stdio.h>
# include <strings.h>
# include <grp.h>

extern char	*getlogin ();

char	*basename, *cp, **cpp,
	*holidays = "/usr/games/lib/holidays",	/* format is dd/mm/yy */
	holbuf [10], realgame [100] = "/usr/games/bin/",
	*sorry [] =
	{ "Games may not be played on working days between 09:00 and 17:00.\n",
	  0
	},
	*scorers [] =	/* games which use "-s" to print scores */
	{ "rogue",
	  "robots",
	  "hack",
	  "rogomatic",
	  0
	};

int	fd = -1, wizard, group, ngroups = NGROUPS, goodguy, glist [NGROUPS];
/* rest of program deleted for clarity */

"wizard.c", line 25: error: syntax error
"wizard.c", line 25: error: syntax error
"wizard.c", line 25: error: syntax error
"wizard.c", line 25: error: syntax error: type expected for vector glist
4 errors
-- 
EMAIL:	stephen@comp.lancs.ac.uk	| Post: University of Lancaster,
UUCP:	...!mcvax!ukc!dcl-cs!stephen	|	Department of Computing,
Phone:	+44 524 65201 Ext. 4120		|	Bailrigg, Lancaster, UK.
Project:Alvey ECLIPSE Distribution	|	LA1 4YR

karl@haddock (08/16/86)

dcl-cs!stephen writes:
>C++ is supposed to be upwards compatible with C.  Can anybody, therefore,
>explain why the following does not compile (under 4.2):
[irrelevant portions deleted --karl]
># include <grp.h>
>int    fd = -1, wizard, group, ngroups = NGROUPS, goodguy, glist [NGROUPS];

Because <grp.h> declares "struct group", and line 25 declares "int group".
In C++, class names share the same namespace as variables -- there is, in
effect, an automatic declaration "typedef struct group group".  I think there
might be a compatibility flag to allow the collision.

In future, you might try isolating the problem by deleting chunks of code
until you have a minimal-case program that demonstrates the alleged bug.  It
makes it easier to convince the Powers That Be if it is a compiler bug, or
to find it yourself if it isn't.

Karl W. Z. Heuer (ihnp4!ima!haddock!karl), The Walking Lint