Passing objects to cpp classes

Hi there,

I guess I have a quite simple question for you guys but it drives me nuts. Here is what i want to do or at least it looks like the same in my code.

myclassA.h

#ifndef MYCLASSA
#define MYCLASSA

#include "Arduino.h"

class myclassA
{
  public:
    myclassA();
    void DoSomething(void);
};
#endif

myclassB.h

#ifndef MYCLASSB
#define MYCLASSB

#include "Arduino.h"
#include "myclassA.h"

class myclassB
{
  public:
    myclassB();
    void DoSomething(myclassA& classA);
};
#endif

main code

#include "myclassA.h"
#include "myclassB.h"

myclassA p_classA();
myclassB p_classB();

void setup()
{}

void loop()
{
   p_classB.DoSomething(p_classA);
}

This is just an abstract example of what Iam trying to do, but in my code I get the following error as soon as myclassB::DoSomething(..) has the argument as described above.

no matching function for call to 'myclassB::DoSomething(myclassA (&)())'

What the hell I am doing wrong?

What is the compiler trying to tell me with the "(&)()" in the error message?

The line

void DoSomething(myclassA& classA);

is a method declaration only. Where your definition of it??

I only posted an "abstract" of my code. There are also cpp files with the corresponding implementation of the function.
Does this answer the question?

This code compiles fine for me:

class myclassA
{
  public:
    myclassA() {};
    void DoSomething(void);
};
class myclassB
{
  public:
    myclassB() {};
    void DoSomething(myclassA& classA) {};
};

myclassA p_classA;
myclassB p_classB;

void setup()
{}

void loop()
{
   p_classB.DoSomething(p_classA);
}

I added implementations to the constructors and methods and removed empty braces after object instantes.

I use Arduino v1.8.19

I think this is the problem (you declare a function that takes no arguments and returns an object of type myclassA).

This is a common mistake:

If the constructor takes no arguments you CAN'T use the empty parentheses: (). If there is a type declaration in the parens the compiler knows you are declaring a function with an argument. If there is a value in the parens the compiler knows you are calling a function. When there is nothing in the parens the compiler can't tell for sure so it defaults to declaring a function, but you want to call the constructor, not declare it. You have to leave the parens out.

Oh wow. That was the problem. Thank you so much.

But just to understand this. I am telling the compiler that I want to create an object of the type myClassA, so he should know that I declared a constructor for it, shouldn't he?

Thanks so much. That was the problem!

Yes it does, but what if you wanted to declare a function that returns an object of type myclassA?

Perhaps you can mark the most helpful post as the solution then? This prevents helpers spending time on a solved issue and it will lead people with the same question to the correct answer directly.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.