0

I am trying to code a c++ example of two clases, one storing a reference to an object of the other class for an arduino.

My minimal code so far is not working out quite, hope for any pointers (smirk) to the solution here:

main.cpp:

#include <Arduino.h>
#include <ClassA.h>
#include <ClassB.h>

void setup() {
  // put your setup code here, to run once:
  ClassA clA();
  ClassB clB(clA);
};

void loop() {
  // put your main code here, to run repeatedly:
};

classA.h:

#ifndef H_CLASSA
#define H_CLASSA

class ClassA
{
private:
    /* data */
public:
    ClassA();
    ~ClassA();
};

#endif

classA.cpp:

#include <ClassA.h>

ClassA::ClassA()
{
};

ClassA::~ClassA()
{
};

classB.h

#ifndef H_CLASSB
#define H_CLASSB

#include <ClassA.h>

class ClassB
{
private:
    ClassA& _a;
public:
    ClassB(ClassA& a);
    ~ClassB();
};

#endif

ClassB.cpp

#include <ClassB.h>
#include <ClassA.h>

ClassB::ClassB(ClassA &a)  : 
_a(a)
{
}

ClassB::~ClassB()
{
}

The error is shown in the main here: ClassB clB(clA);

Keine Instanz des Konstruktors ""ClassB::ClassB"" stimmt mit der Argumentliste überein. -- Argumenttypen sind: (ClassA ())

When compiling, the following error is thrown:

src\main.cpp: In function 'void setup()':
src\main.cpp:8:17: error: no matching function for call to 'ClassB::ClassB(ClassA (&)())'
   ClassB clB(clA);
                 ^
In file included from src\main.cpp:3:0:
src/ClassB.h:11:5: note: candidate: ClassB::ClassB(ClassA&)
     ClassB(ClassA& a);
     ^~~~~~
src/ClassB.h:11:5: note:   no known conversion for argument 1 from 'ClassA()' to 'ClassA&'
src/ClassB.h:6:7: note: candidate: constexpr ClassB::ClassB(const ClassB&)
 class ClassB
       ^~~~~~
src/ClassB.h:6:7: note:   no known conversion for argument 1 from 'ClassA()' to 'const ClassB&'

Anyone got an idea about what is going on here? Thank you for any help!

juicyCode
  • 3
  • 1

1 Answers1

0

try replacing this

void setup() {
  // put your setup code here, to run once:
  ClassA clA();
  ClassB clB(clA);
};

with this

void setup() {
  // put your setup code here, to run once:
  ClassA clA;
  ClassB clB(clA);
};

why:

when you do

ClassA clA();

you are not creting an instance of the class A but instead is interpreted as a function declaration called clA which returns a ClassA and requires no arguments (()).

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97