Thread: Optional Arguments

  1. #1
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718

    Optional Arguments

    Code:
    // Header file:
    void GetInput(string ErrMsg = "");
    
    //Source file:
    void GetInput(string ErrMsg = "")
    {
    	int iTempX = 0;
    	int iTempY = 0;
    
    	char cTempX = 'A';
    
    	cout<<"Please enter the x-coord: ";
    	cin>>cTempX;
    	
    	iTempX = cTempX - 'A';
    
    	cout<<"Please enter the y-coord: ";
    	cin>>iTempY;
    
    	iTempY--;	
    }
    I get the error "GetInput' : redefinition of default parameter : parameter 1".
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    If my memory serves me correctly (which it probably doesn't), then you can only initialise default parameters in the definition. Try losing the default parameter from the (C++) declaration.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    If I change the function prototype to 'void GetInput(string ErrMsg)', then when I call it using 'GetInput();', I get the following error message: 'error C2660: 'GetInput' : function does not take 0 parameters'
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    Then you haven't given it a default parameter.

    This works -

    Code:
    #include <iostream>
    
    using namespace std;
    
    // Header file:
    void GetInput(string ErrMsg);
    
    //Source file:
    void GetInput(string ErrMsg = "")
    {
    	int iTempX = 0;
    	int iTempY = 0;
    
    	char cTempX = 'A';
    
    	cout<<"Please enter the x-coord: ";
    	cin>>cTempX;
    	
    	iTempX = cTempX - 'A';
    
    	cout<<"Please enter the y-coord: ";
    	cin>>iTempY;
    
    	iTempY--;	
    }
    
    
    int main()
    {
    	GetInput();
    	return 0;
    }

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    That works IF they are in the same file. But if the prototype is in "theheader.h", the code is in "theheader.cpp", and int main() is in "main.cpp", it doesn't work.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Drop the default parameter in the source file fn definition - only have the default in the header file fn declaration.

    (BTW, given that it looks like you've tried just about every other permutation, I would imagine you've already arrived at this conclusion. ).

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Actually, I hadn't tried that.

    It works now. Thanks.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  2. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  3. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  4. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM
  5. Optional arguments
    By Boksha in forum C++ Programming
    Replies: 3
    Last Post: 06-01-2002, 10:37 AM