Constructor using Pointers as parameters

Mar 14, 2017 at 8:01pm
Hello everyone
So I have a probleme with a constructor of a class that takes as paramters 2 pointers on that same class.
I want to initialize those paramters to nullptr as follows:
NameClass(NameClass* name1=nullptr,NameClasse* name2=nullptr)
: ... { // }
I have a compilation error that states "default argument has been given to paramter 1"
Can someone help me with this?
Thanks
Mar 14, 2017 at 10:03pm
Why is your class constructor taking two class pointers?

From what I can see you probably had a constructor that had only 1 parameter and you're trying to declare a constructor with two parameters. You can't set the NameClass* to nullptr inside the parameter.
Mar 14, 2017 at 11:18pm
Ok so how can i use the constructor to tackle both cases
the call NameClass() and NameClass(param1,param2) with pointers on classes?
The same way we would declare Nameclass(int a=0,int b=0) that could be called Nameclass() (zeros assigned to a and b ) or Nameclass(value1,value2) ?

Thanks for the help
Mar 15, 2017 at 4:21am
there is probably a way to do an initialization list but you can just do

classname(classname*a, classname*b)
{
a = b = null;
}
Mar 15, 2017 at 10:43am
Only obvious problem I can see with the code is that NameClasse is misspelled.

The default arguments should only be provided when declaring the function inside the class definition. If you are defining the function outside the class definition you should not repeat the default arguments.

1
2
3
4
5
6
7
8
class NameClass
{
	NameClass(NameClass* name1=nullptr,NameClass* name2=nullptr); // Default arguments there.
};

NameClass::NameClass(NameClass* name1, NameClass* name2) // No default arguments there.
{
}
Last edited on Mar 15, 2017 at 10:43am
Topic archived. No new replies allowed.