-
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".
-
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.
-
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'
-
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;
}
-
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.
-
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. ;) ).
-
Actually, I hadn't tried that. :D
It works now. Thanks.