1

I have two classes in my sketch that are independent of each other (a serial output and an LCD display) A simplified structure of my code is as follows:

class A
{
  public:
   int x;
   void afoo()
   {
     x=1;
   }
   void afoo2
   {
     x=ClassB.bfoo+1;
   }
};

A ClassA;

class B
{
  public:
    int y;
    void bfoo()
    {
      y=ClassA.afoo+2;
    }
};
B ClassB;

However, if I don't comment out class afoo2 I get the following error:

exit status 1

'ClassB' was not declared in this scope

I'm guessing that this is because I haven't yet created B ClassB while class is being defined. Is there a way I can do what I'm intending here?

3

You can't see non-static member variables in a class like that. The closest is to make it static, which means you get one and only one copy of the variable across all instances of your class:

class ClassB;
class ClassA {
    public:
        int x;
        void foo();

};
class ClassB {
    public:
        static int bfoo;
};

void ClassA::foo() {
    x = ClassB::bfoo + 1;
}

However I suspect that isn't what you want. I suspect you actually want to see the bfoo value stored in class instance B - so you need to tell you classA instance A what and where B is. To do that you will have to pass it when constructing or during some other setup phase. If you want A to know B and B to know A you can't easily do it during construction, so you will have to do it at a later stage:

class ClassB;

class ClassA {
    public:
        ClassB *B;
        int fooa;
        void begin(ClassB *b) {
            B = b;
        }
        void foo();
};
class ClassB {
    public:
        ClassA *A;
        int foob;
        void begin(ClassA *a) {
            A = a;
        }
        void foo();
};

void ClassA::foo() {
    fooa = B->foob + 1;
}

void ClassB::foo() {
    foob = A->fooa + 1;
}

ClassA A;
ClassB B;

void setup() {
    A.begin(&B);
    B.begin(&A);
}

void loop() {

}
| improve this answer | |
  • Ok, why do you have class ClassB; and class ClassA A at the beginning as well as before the setup? – ATE-ENGE Jul 26 '17 at 16:21
  • 1
    class ClassB; means "ClassB exists, but I am going to define it later on." It allows ClassA to know about the type ClassB even though ClassB hasn't been defined in the file yet. – Majenko Jul 26 '17 at 16:23
  • trying your sketch, I'm getting an error forward declaration of 'class ClassB' – ATE-ENGE Jul 26 '17 at 17:25
  • Actually, the foo() functions can't be contained within the class structure as I have it - they have to be external to it so that it knows the structure of the classes before they are used. I will edit a working sketch in, – Majenko Jul 26 '17 at 17:52
1

What you are trying to do is call class A from B and vice versa.

What you could try, is remove one of the functions out of the class, possibly to a third class, so class A and B depend on a class C and not on each other.

However, if they are really depend, you can split up the files in header/implementation files.

Instead of using two classes directly, pass them as a parameter (reference).

Somewhat like this (not tested)

File A.h:

class A
{
  public:
   int x;
   void afoo();
   void afoo2(B& b);
};

File A.cpp:

#include "A.h" 
#include "B.h" 

void A::afoo()
{
  x=1;
}

void A::afoo2(B& b)
{
  x=b.bfoo+1;
}

file B.h:

class B
{
  public:
    int y;
    void bfoo(A& a);
}

file B.cpp:

#include "A.h" 
#include "B.h" 

B::void bfoo(A& a)
{
  y=a.afoo+2;
}

And your ino file you declare A and B.

#include "A.h"
#include "B.h"

A ClassA;
B ClassB;

ClassA.afoo(ClassB);
ClassB.bfoo(ClassA);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.