LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-31-2007, 07:29 PM   #1
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Rep: Reputation: 30
Predefined function parameter giving problems.


Hello, I got a problem with with my header file.

It has an external reference to a function, which that function has a parameter that is predefined.

Here is the neccessary information the source file has (.cpp) followed by the header file (.h):


Source:
Code:
//...

int function(/*...*/, int number);

//...

int function(/*...*/, int number = 0)
{
    //...
}

//...
Header:
Code:
//...

extern int function(/*...*/, int number = 0);

//...
Here is the error I recieve when I compile my program:

Code:
g++ -c p_datamanip.cpp p_error.h -g -W -Wall -ansi -pedantic `sdl-config --cflags --libs` -lSDL
p_datamanip.cpp: In function `int p_fileread_quote(std::string, std::string, std::string&, unsigned int)':
p_datamanip.cpp:115: error: default argument given for parameter 4 of `int p_fileread_quote(std::string, std::string, std::string&, unsigned int)'
p_datamanip.cpp:23: error: after previous specification in `int p_fileread_quote(std::string, std::string, std::string&, unsigned int)'
make: *** [p_datamanip.o] Error 1
What should I do?
 
Old 01-31-2007, 10:36 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
If I understand what you have done correctly, the compiler can't tell the difference between the two functions. To overload a function the parameter list must differ in the number of parameters or in the type order.

So the following would be valid:

int fn (string name);
int fn (string name , int id); // different number
int fn (int id, string name); // different order

What you can't do is to just change the return type, the name of the parameter or add a default value

So the following would be invalid (assuming that the above three have been defined):

void fn (string); // change in return type not valid
int fn (string name, int amount); // change parameter name not valid
int fn (string name , int id=0); // add a default value not valid

What can you do?

If it is just a case of a default value and the logic is the same then replace the function signature that doesn't have a default value with one that does.

If it is truly performing some different logic then give it a different name

If it is a case when it wants some different data but performs the same logic then you could add an intermediate function to make that clear, but this new function will use the original function.

I'm sure there are many other alternatives but without more understanding of the problem at hand...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
what is function parameter? Jeon, Chang-Min Linux - Software 2 06-22-2006 06:53 PM
Passing one php function result as a parameter to another php function davee Programming 13 09-12-2004 12:08 PM
How to change parameter values of a function in shell script? Bassam Programming 0 01-25-2004 09:52 AM
C: struct as a function parameter zokik Programming 3 12-06-2003 02:52 PM
Bash function parameter Misel Programming 2 05-17-2003 11:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration