forcing constructor to use a default parameter

Hi,

I am trying to initialize the second constructor by forcing the constructor to use just 1 parameter instead of 2. I expected 3,8 to be passed from Foo goo.

works fine:
Foo foo;
foo.makeNestedloop(7,5); //works fine

error:
Foo goo;
goo.makeNestedloop(3);// no defult constructor applied

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
#include<iostream>

class Foo
{
private:
	int outer=6;
	int inner=8;

public:
makeNestedloop(int inner, int outer)
{
for(int i(0); i<=inner;++i ) //outer
		{
			std::cout<<i<<"  ";
			 for (int j(0); j<=outer;++j)//inner
			{
				 std::cout<<j<<" ";
			}
			 std::cout<<std::endl;
		}
}
};

int main()
{

Foo foo;
foo.makeNestedloop(7,5); //works fine

Foo goo;
goo.makeNestedloop(3);//errors

}


[output]
In function 'int main()':
..\src\main.cpp:32:21: error: no matching function for call to 'Foo::makeNestedloop(int)'
goo.makeNestedloop(3);//
[output]
Last edited on
I suppose you are trying to make a default parameter

its syntax is somewhat like this

 
makeNestedloop(int inner=8, int outer=6)


further reading : http://en.cppreference.com/w/cpp/language/default_arguments
line 10: makeNestedloop needs a return type. Since you're not returning anything, it should be type void.
Topic archived. No new replies allowed.