Strange class problem

I am trying to write some classes (for a walking robot, but that's besides the point), and I am having some unexpected problems.

I created the following class which works fine:

class MyServo {
	public:
		MyServo(Servo s, int ce, int hi, int lo, bool inv);
		............
};

But then I created another class which takes MyServo objects in its constructor:

class Leg {
	public:
		Leg(MyServo s1, MyServo s2, MyServo s3);
		............
};

This didn't cause any problems either, but then I wrote the actual constructor:

Leg::Leg(MyServo s1, MyServo s2, MyServo s3) {
	............
}

And this gave me the following error: "No matching function for call to MyServo::MyServo".

I tried figuring this out but I must admit I don't understand it. In particular because the MyServo constructor takes a Servo object as an argument, which to me is exactly the same situation as the Leg constructor taking a MyServo object as an argument.

Here is the entire error message:

NewCrab.ino: In constructor 'Leg::Leg(MyServo, MyServo, MyServo)':
NewCrab:130: error: no matching function for call to 'MyServo::MyServo()'
NewCrab.ino:93: note: candidates are: MyServo::MyServo(Servo, int, int, int, bool)
NewCrab.ino:78: note:                 MyServo::MyServo(const MyServo&)
NewCrab:130: error: no matching function for call to 'MyServo::MyServo()'
NewCrab.ino:93: note: candidates are: MyServo::MyServo(Servo, int, int, int, bool)
NewCrab.ino:78: note:                 MyServo::MyServo(const MyServo&)
NewCrab:130: error: no matching function for call to 'MyServo::MyServo()'
NewCrab.ino:93: note: candidates are: MyServo::MyServo(Servo, int, int, int, bool)
NewCrab.ino:78: note:                 MyServo::MyServo(const MyServo&)

PS: I have attached the code in an file...

NewCrab.ino (4.44 KB)

Ok it appears I have allready solved this problem by adding an empty default contructor for the MyServo class. I don't really like this solution and I don't understand it well, so I would still like a bit of input from someone who does....

I don't really like this solution and I don't understand it well, so I would still like a bit of input from someone who does....

You can't pass an object to a function unless you have an instance of the object to pass. If you do, passing the object to the function doesn't make sense. You would typically pass the object by reference or by passing a pointer to the object.

What adding the no argument constructor did was to allow you to create objects on the fly. Generally, this is NOT what you want, since the object is supposed to represent some real world object. The abstract MyServo object, with no Servo object, no position data, etc. is pretty useless.

The code snippets you provided do not provide near enough information to let us understand what you are doing wrong, or to suggest, in more than very general terms, how to fix the problem.

Hi thanks a lot for the reply...

Not sure what you mean by: "You can't pass an object to a function unless you have an instance of the object to pass."

The error occurred not when passing the object but when writing the function definition. I simply want the function to take arguments of the type MyServo. But apparently in this context MyServo isn't treated as a type, which it is in other contexts, but rather as a constructor.

"The code snippets you provided do not provide near enough information to let us understand what you are doing wrong, or to suggest, in more than very general terms, how to fix the problem."

Ah but I attached the entire code as a file, since it would be too long to paste directly into the thread.

BTW the solution worked well enough, I just don't understand the theory and I would very much like too.

Again thanks for the reply...

The error occurred not when passing the object but when writing the function definition.

C and C++ are pass-by-value languages. To pass an object, an object is created and then the copy constructor is called to properly populate it with the data of the object used in the call. That requires a no argument constructor.

It's better to circumvent the pass-by-value nature of C by using references or pointers.