g++ 3.3.2 unsuports defaults aragument, does it ?

Y

Yuichiro

Hello,

I need your help.
I am trying to use g++ 3.3.2. But I have got a big problem.
Does g++ 3.3.2 support default arguments ?

I wrote a simple c++ source as below.
The compiler returns:

error: default argument given for parameter 3 of `int func(int, int,
int)'
What causes this ?

I am migrating c++ programs written in g++ 2.7.2.3 to g++ 3.3.2.
Some functions containing defaults arguments are used in the programs.
I am at my wit's end.

Any advice would be appreciated.
Thanks in advance.

Yuichiro


default.cc
------------
#include <iostream>

using namespace std;
int func(int a, int b, int n=1);

int main()
{
int t;

t = func(10, 20);
cout << t << endl;

return 0;
}

int func(int a, int b, int n=1)
{
return 100 * n;
}
------------

% g++ default.cc
default.cc: In function `int func(int, int, int)':
default.cc:17: error: default argument given for parameter 3 of `int
func(int, int, int)'
default.cc:4: error: after previous specification in `int func(int,
int, int)'
------------
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Yuichiro said:
Hello,

I need your help.
I am trying to use g++ 3.3.2. But I have got a big problem.
Does g++ 3.3.2 support default arguments ?

I wrote a simple c++ source as below.
The compiler returns:

error: default argument given for parameter 3 of `int func(int, int,
int)'
What causes this ?

I am migrating c++ programs written in g++ 2.7.2.3 to g++ 3.3.2.
Some functions containing defaults arguments are used in the programs.
I am at my wit's end.

Any advice would be appreciated.
Thanks in advance.

Yuichiro


default.cc
------------
#include <iostream>

using namespace std;
int func(int a, int b, int n=1);

int main()
{
int t;

t = func(10, 20);
cout << t << endl;

return 0;
}

int func(int a, int b, int n=1)
{
return 100 * n;
}
------------
Give the default in the declaration, not the definition of the function.
 
J

John Harrison

Yuichiro said:
Hello,

I need your help.
I am trying to use g++ 3.3.2. But I have got a big problem.
Does g++ 3.3.2 support default arguments ?

I wrote a simple c++ source as below.
The compiler returns:

error: default argument given for parameter 3 of `int func(int, int,
int)'
What causes this ?

I am migrating c++ programs written in g++ 2.7.2.3 to g++ 3.3.2.
Some functions containing defaults arguments are used in the programs.
I am at my wit's end.

Any advice would be appreciated.
Thanks in advance.

Yuichiro


default.cc
------------
#include <iostream>

using namespace std;
int func(int a, int b, int n=1);

int main()
{
int t;

t = func(10, 20);
cout << t << endl;

return 0;
}

int func(int a, int b, int n=1)
{
return 100 * n;
}

It's not legal to repeat a default parameter. If you give the default value
in the prototype, don't repeat it in the definition.

One of the weaknesses of g++ is that it accepts all sorts of illegal code.
There are compiler switches to stop that, you would do well to use them.

john
 
Y

Yuichiro

Thank you for your advice. It helped me solve the problem.
Thanks a lot !

Yuichiro
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,530
Latest member
jameswilliam1

Latest Threads

Top