GCC default parameter error

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
firas981

GCC default parameter error

Post 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++ .
proxy

Re:GCC default parameter error

Post 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
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:GCC default parameter error

Post 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
Cemre

Re:GCC default parameter error

Post 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
}
proxy

Re:GCC default parameter error

Post 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
firas981

Re:GCC default parameter error

Post 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 .
proxy

Re:GCC default parameter error

Post 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
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:GCC default parameter error

Post 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...
Post Reply