Page 1 of 1
GCC default parameter error
Posted: Mon May 31, 2004 6:07 pm
by firas981
in vga.h I have :
void scroll ( int lines = 1 ) ;
in vga.c I have :
void scroll ( int lines )
{
//implementation
}
GCC gave me the error "Parse error before =" at the line containing the prototype in vga.h .
the only way to correct the error is to delete " = 1 " from prototype , but I want to use default values ,
I am sure that this way was successful in MS visual c++ & borland c++ .
Re:GCC default parameter error
Posted: Mon May 31, 2004 8:06 pm
by proxy
default values for parameters are a feature present in C++, not C.
if you intended to use C++ then you shouldn't give your file a ".c" extention, but either ".C" or ".cpp" or ".cxx"
proxy
Re:GCC default parameter error
Posted: Tue Jun 01, 2004 3:08 am
by Pype.Clicker
firas981 wrote:
I am sure that this way was successful in MS visual c++ & borland c++ .
Those compilers might have different ways of 'guessing' if some code is C or C++ ... or simply use the C++ compiler for C code aswell ...
If renaming the source files is not an option for you, you may wish to use
-x c++ flag in your GCC command line
Re:GCC default parameter error
Posted: Tue Jun 01, 2004 9:54 am
by Cemre
in vga.h I have :
void scroll ( int lines = 1 ) ;
in vga.c I have :
void scroll ( int lines )
{
//implementation
}
this is the wrong way to do it...
do the following...
the default parameter MUST be given
at the implementation, NOT in the
prototype
in vga.h I have :
void scroll ( int lines ) ;
in vga.c I have :
void scroll ( int lines = 1 )
{
//implementation
}
Re:GCC default parameter error
Posted: Tue Jun 01, 2004 2:06 pm
by proxy
Cemre, i am sorry but you are mistaken, while it is allowed to have it in the definition and not the prototype, it may be in either but not both. (see
http://cplus.about.com/library/weekly/aa061102f.htm).
However, it is better convention to put it in the prototype, here is why.
typically when delivering a library, you deliver a binary source and headers. if the defaults are defined in the source but not the headers, how would the client ever know what the default value is or that there is any at all
proxy
Re:GCC default parameter error
Posted: Tue Jun 01, 2004 4:33 pm
by firas981
Thanks you all , well , I'll continue using c and delete default parameters , but , I want to say that the default parameters
appear only one time ,not more at all . and this "one-time " should be the first time that the function appear which is usually ( but not always) the prototype . I am sure that this is true at least in C++ compilers under Windows that I mentioned before .
and "How to Program in C++" supports my opinion .
Thanks .
Re:GCC default parameter error
Posted: Tue Jun 01, 2004 5:40 pm
by proxy
of course the C++ compilers supports it, like i said it is "a feature of C++ not C"
the problem is that your file is a .c file and any self respecting compiler will default to C mode for that, not C++.
proxy
Re:GCC default parameter error
Posted: Wed Jun 02, 2004 1:07 am
by Candy
proxy wrote:
the problem is that your file is a .c file and any self respecting compiler will default to C mode for that, not C++.
For which I'm very happy that GCC can be forced to do C++, seeing as I would like my C code to compile with C++-esque name mangling...