LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 03-07-2007, 07:51 PM   #1
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Rep: Reputation: 30
C++: How to have predefined function parameters in source and header files...


I have a function that I want to use throughout my program, the problem is I want a function parameter to be predefined. The problem is gcc is telling me that the function parameter has been defined differently in different areas. (the source and the header) Maybe someone can help:

Source File Prototype:

Code:
int p_find_around(std::string in_this, std::string find_what, unsigned int what_iter,
std::string& send_to, std::string around = "\"");
Source File Full Function:

Code:
int p_find_around(std::string in_this, std::string find_what, unsigned int what_iter,
std::string& send_to, std::string around = "\"")
Header File:

Code:
extern int p_find_around(std::string in_this, std::string find_what, unsigned int
what_iter, std::string& send_to, std::string around = "\"");
I get this when I Make my program:

Code:
p_datamanip.cpp: In function `int p_find_around(std::string, std::string, unsigned int,
std::string&, std::string)':
p_datamanip.cpp:120: error: default argument given for parameter 5 of `int
p_find_around(std::string, std::string, unsigned int, std::string&, std::string)'
p_datamanip.cpp:24: error: after previous specification in `int
p_find_around(std::string, std::string, unsigned int, std::string&, std::string)'
Any ideas? Thanks in advance!

Last edited by RHLinuxGUY; 03-07-2007 at 07:57 PM.
 
Old 03-07-2007, 10:18 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 111Reputation: 111
Get rid of the default parameter in the implementation files. BTW, something about this feels very unlike C++, more like C.
 
Old 03-07-2007, 11:19 PM   #3
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Original Poster
Rep: Reputation: 30
The problem now is that gcc is telling me that all iterations of this function has too few arguments.
 
Old 03-08-2007, 12:04 AM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 111Reputation: 111
How many arguments are you passing it? Should be 4 or 5. Exact error message please?
 
Old 03-08-2007, 05:28 PM   #5
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Original Poster
Rep: Reputation: 30
Here:

Code:
p_resources.h: In member function `int P_RESOURCES::allocate_fonts()':
p_datamanip.h:31: error: too few arguments to function `int p_fileread_around(std::string, std::string, std::string&, unsigned int, std::string)'
p_resources.h:107: error: at this point in file
p_datamanip.h:31: error: too few arguments to function `int p_fileread_around(std::string, std::string, std::string&, unsigned int, std::string)'
p_resources.h:110: error: at this point in file
p_resources.h: In member function `int P_RESOURCES::allocate_images()':
p_datamanip.h:31: error: too few arguments to function `int p_fileread_around(std::string, std::string, std::string&, unsigned int, std::string)'
p_resources.h:146: error: at this point in file
p_datamanip.h:31: error: too few arguments to function `int p_fileread_around(std::string, std::string, std::string&, unsigned int, std::string)'
p_resources.h:153: error: at this point in file
std::string)'
 
Old 03-08-2007, 08:01 PM   #6
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 111Reputation: 111
That says p_fileread_around, not p_find_around.
 
Old 03-08-2007, 10:15 PM   #7
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Original Poster
Rep: Reputation: 30
Oops, it looks like it is failing when it sees and does it's best to make sense of `int p_fileread_around(...)'. Which happens to contain `p_find_around(...)'.

So here is the code for that function. Which is more or less the same as the one before. Exactly the same problem, if I am not mistaken.

Source File Prototype:

Code:
int p_fileread_around(std::string from_file, std::string find_what, std::string& to_sz,
unsigned int what_iter, std::string around);
Source File Full Function:

Code:
int p_fileread_around(std::string from_file, std::string find_what, std::string& to_sz,
unsigned int what_iter, std::string around = "\"")
Header File:

Code:
extern int p_fileread_around(std::string from_file, std::string find_what,
std::string& to_sz, unsigned int what_iter, std::string around);
 
Old 03-09-2007, 03:04 AM   #8
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Default argument shoudl only be given in the header files, it looks as if you have a defautl value in the .cpp file.
 
Old 03-09-2007, 04:48 PM   #9
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Original Poster
Rep: Reputation: 30
Done... here is the output.

Code:
p_datamanip.h:30: error: default argument given for parameter 5 of `int
p_find_around(std::string, std::string, unsigned int, std::string&, std::string)'
p_datamanip.h:30: error: after previous specification in `int p_find_around(std::string,
std::string, unsigned int, std::string&, std::string)'
p_datamanip.h:31: error: default argument given for parameter 5 of `int
p_fileread_around(std::string, std::string, std::string&, unsigned int, std::string)'
p_datamanip.h:31: error: after previous specification in `int
p_fileread_around(std::string, std::string, std::string&, unsigned int, std::string)'
 
Old 03-09-2007, 09:50 PM   #10
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Okay, but what does the code now look like? Especially lines 30 and 31 of p_datamanip.h
 
Old 03-09-2007, 11:41 PM   #11
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Original Poster
Rep: Reputation: 30
Code:
extern int p_find_around(std::string in_this, std::string find_what, unsigned int
what_iter, std::string& send_to, std::string around = "\""); // Line 30
extern int p_fileread_around(std::string from_file, std::string find_what, std::string&
to_sz, unsigned int what_iter, std::string around = "\""); // Line 31
The other two lines are in the source file, and just have the predefinition removed.

Last edited by RHLinuxGUY; 03-09-2007 at 11:42 PM.
 
Old 03-10-2007, 12:03 AM   #12
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
you have extern, so I guess that the function is declared elsewhere? If that is the case you can't use default parameters here. You need to put them at the original point of declaration.

Last edited by graemef; 03-10-2007 at 12:10 AM.
 
Old 03-10-2007, 02:54 PM   #13
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Original Poster
Rep: Reputation: 30
I'm back to what Tuxdev told me to do.

Here is the output now:

Code:
p_datamanip.h:31: error: too few arguments to function `int p_fileread_around(std::string, std::string, std::string&, unsigned int, std::string)'
p_resources.h:107: error: at this point in file
I'm not giving a 5th argument to the function, obviously because it doesn't see it being predefined in the header file.

Last edited by RHLinuxGUY; 03-10-2007 at 02:59 PM.
 
Old 03-10-2007, 11:37 PM   #14
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
I've not seen enough of teh code to fully understand what is going on. But I think that p_fileread_around is declared somewhere, it is being redeclared as extern in p_datamanip.h. So when you redeclare it the number of parameters must match with the original declaration without the default parameters. So in p_datamanip.h you want:
Code:
extern int p_fileread_around(std::string, std::string, std::string&, unsigned int, std::string);
 
Old 03-11-2007, 04:42 AM   #15
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Original Poster
Rep: Reputation: 30
Here is how the source file is setup:

Code:
#include ...

// prototype
int int p_fileread_around(std::string, std::string, std::string&, unsigned int, std::string);

// ...

int p_fileread_around(std::string from_file, std::string find_what, std::string&
to_sz, unsigned int what_iter, std::string around = "\"");
{

}
Header file:

Code:
#include ...

extern int int p_fileread_around(std::string, std::string, std::string&, unsigned int, std::string);
If you would like more information, just ask. Thanks for the help so far!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Predefined function parameter giving problems. RHLinuxGUY Programming 1 01-31-2007 10:36 PM
JS: assigning a function call with parameters to an event eantoranz Programming 1 10-09-2006 03:05 PM
Including header files and source files for classes Feenix Programming 8 09-28-2005 10:53 AM
"Cannot find header source files" when trying to install Nvidia 6111 drivers naddad Linux - Newbie 3 08-31-2004 12:42 PM
c header files in linux in place of header files in windows? harun_acs Programming 1 03-17-2004 02:24 AM


All times are GMT -5. The time now is 06:46 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration