OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Apr 19, 2024 3:24 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: GCC default parameter error
PostPosted: Mon May 31, 2004 6:07 pm 
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++ .


Top
  
 
 Post subject: Re:GCC default parameter error
PostPosted: Mon May 31, 2004 8:06 pm 
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


Top
  
 
 Post subject: Re:GCC default parameter error
PostPosted: Tue Jun 01, 2004 3:08 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 2:31 am
Posts: 5964
Location: In a galaxy, far, far away
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

_________________
Image May the source be with you.


Top
 Profile  
 
 Post subject: Re:GCC default parameter error
PostPosted: Tue Jun 01, 2004 9:54 am 
Quote:

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

Quote:
in vga.h I have :
void scroll ( int lines ) ;
in vga.c I have :
void scroll ( int lines = 1 )
{
//implementation
}


Top
  
 
 Post subject: Re:GCC default parameter error
PostPosted: Tue Jun 01, 2004 2:06 pm 
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


Top
  
 
 Post subject: Re:GCC default parameter error
PostPosted: Tue Jun 01, 2004 4:33 pm 
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 .


Top
  
 
 Post subject: Re:GCC default parameter error
PostPosted: Tue Jun 01, 2004 5:40 pm 
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


Top
  
 
 Post subject: Re:GCC default parameter error
PostPosted: Wed Jun 02, 2004 1:07 am 
Offline
Member
Member
User avatar

Joined: Tue Oct 17, 2006 11:33 pm
Posts: 3882
Location: Eindhoven
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...


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], FrankRay78 and 151 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group