optional function parameters

i have done some research and i've found explanations of how to make a parameter in a function options:

int function(int optional=3){return 0;}

BUT, I have a class object I am passing to a parameter that I would like to make optional. This object is defined in the constructor as: double (*data)[ROW][COL]=new double [numSeasonDays][ROW][COL];- this is filled in and passed to a write function. I have to write several different kinds of class objects (all in this format, but of different sizes) to a NetCDF file- because i am working with HUGE data sets, i want to write these as I finish using them to make more space for the new objects I create.

void write_MCIP (int var,int ini_ffmc, int ini_dmc,int ini_dc,char filename[],timeStamp& date=NULL,FireWeatherMCIP& variable=NULL,seasonStartMCIP& initialize_variable=NULL);

doesn't work... I get the following errors:
error: default argument for â has type â
error: aggregate â has incomplete type and cannot be defined
error: variable or field â declared void


help?
I think references can't be null, only pointer can.
If you make them pointers that may fix the problem.

Edit: I know references can't be null, I mean I think that's your problem. :p
Last edited on
I'll try that, but let's say I want to keep it a reference, do you know what I'd do?
Yes but the easy way will still involve pointers:
Then you need to overload the function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void write_MCIP (int var,int ini_ffmc, int ini_dmc,int ini_dc,char filename[])
{
write_MCIP (var, ini_ffmc, ini_dmc, ini_dc, filename[]);
}
void write_MCIP (int var,int ini_ffmc, int ini_dmc,int ini_dc,char filename[], timeStamp& date);
{
write_MCIP (var, ini_ffmc, ini_dmc, ini_dc, filename[], &date);
}
void write_MCIP (int var,int ini_ffmc, int ini_dmc,int ini_dc,char filename[], timeStamp& date, FireWeatherMCIP& variable)
{
write_MCIP (var, ini_ffmc, ini_dmc, ini_dc, filename[], &date,&variable);
}
void write_MCIP (int var,int ini_ffmc, int ini_dmc,int ini_dc,char filename[],timeStamp& date,FireWeatherMCIP& variable,seasonStartMCIP& initialize_variable);
{
write_MCIP (var, ini_ffmc, ini_dmc, ini_dc, filename[], &date,&variable,&initialize_variable);
}

void write_MCIP (int var,int ini_ffmc, int ini_dmc,int ini_dc,char filename[],timeStamp* date=NULL,FireWeatherMCIP* variable=NULL,seasonStartMCIP* initialize_variable=NULL)
{
// your code here...
}

There are several ways to do this, but what you may find most useful is to keep a couple of objects (their value is not important) in a private place and check to see if they are referenced:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
using namespace std;


// this stuff goes in your header file, of course

struct Foo
  {
  int x;
  int y;
  Foo( int x, int y = 12 ): x( x ), y( y ) { }
  };

extern Foo myfunc_foo_default;

void myfunc( Foo& foo = myfunc_foo_default );


// this stuff goes in your source file, naturally

Foo myfunc_foo_default( 21, -3 );

void myfunc( Foo& foo )
  {
  if (&foo == &myfunc_foo_default)
    cout << "myfunc --> default (" << foo.x << ", " << foo.y << ")\n";
  else
    cout << "myfunc --> "      "(" << foo.x << ", " << foo.y << ")\n";
  }


// and here's your main function

int main()
  {
  myfunc();
  Foo bar( 14, 97 );
  myfunc( bar );
  return 0;
  }

Keep in mind that a "class object" is a user-defined structure -- arrays don't count. If you are just wanting to make a default class, then use a const argument with a default value:

1
2
3
4
void doit( const myclass& quux = myclass( 12, -3, whatever ) )
  {
  ...
  }

It makes no sense to have a default, modifiable argument, but if you really need to, the overload your function (as exampled by Raggers).

Hope this helps.
Topic archived. No new replies allowed.