Skip to main content
added 1 character in body
Source Link
Michel Keijzers
  • 13k
  • 7
  • 42
  • 59

I wanted to move most functionality of my sketch into different classes, to keep things better organized. OnOne class worked just fine, but when I had to use multiple classes and pass information between then things stopped working.

I have two classes, my main Remote class, and my RemoteDisplay class. The main class takes care of fx., measuring battery voltage and I want to display this through the RemoteDisplay class. To do so I initiate the Display class with a pointer to the Remote class, so I can access the voltage function. This, however, doesn't work, even though I forward declare the Remote class in the Display class.

Remote.ino

#include "Remote.h"

Remote Remote;

void setup() {
  Serial.begin(9600);
}

void loop() {
  Remote.display.showBattery();
}

Remote.h

#include "RemoteDisplay.h"
class Remote
{

public: 

  RemoteDisplay display;

  Remote( void );

  float voltage;

};

Remote.cpp

#include "Remote.h"

Remote::Remote(void)
{
  display.initiate(this);
}

RemoteDisplay.h

class Remote;

class RemoteDisplay
{

public:

  RemoteDisplay(void);

  void initiate( Remote * _pointer );

  void showBattery( void );

private:

  /* Reference to the remote class */
  Remote * pointer;
};

RemoteDisplay.cpp

#include "RemoteDisplay.h"

void RemoteDisplay::initiate( Remote * _pointer )
{
  pointer = _pointer;
}

void RemoteDisplay::showBattery( void )
{
  Serial.println( pointer->voltage );
}

I get the errors:

error: invalid use of incomplete type 'class Remote'
showBattery( pointer->voltage() );

note: forward declaration of 'class Remote'
class Remote;

Why is it so? Any help is much appreciated.

I wanted to move most functionality of my sketch into different classes, to keep things better organized. On class worked just fine, but when I had to use multiple classes and pass information between then things stopped working.

I have two classes, my main Remote class, and my RemoteDisplay class. The main class takes care of fx. measuring battery voltage and I want to display this through the RemoteDisplay class. To do so I initiate the Display class with a pointer to the Remote class, so I can access the voltage function. This, however, doesn't work, even though I forward declare the Remote class in the Display class.

Remote.ino

#include "Remote.h"

Remote Remote;

void setup() {
  Serial.begin(9600);
}

void loop() {
  Remote.display.showBattery();
}

Remote.h

#include "RemoteDisplay.h"
class Remote
{

public: 

  RemoteDisplay display;

  Remote( void );

  float voltage;

};

Remote.cpp

#include "Remote.h"

Remote::Remote(void)
{
  display.initiate(this);
}

RemoteDisplay.h

class Remote;

class RemoteDisplay
{

public:

  RemoteDisplay(void);

  void initiate( Remote * _pointer );

  void showBattery( void );

private:

  /* Reference to the remote class */
  Remote * pointer;
};

RemoteDisplay.cpp

#include "RemoteDisplay.h"

void RemoteDisplay::initiate( Remote * _pointer )
{
  pointer = _pointer;
}

void RemoteDisplay::showBattery( void )
{
  Serial.println( pointer->voltage );
}

I get the errors:

error: invalid use of incomplete type 'class Remote'
showBattery( pointer->voltage() );

note: forward declaration of 'class Remote'
class Remote;

Why is it so? Any help is much appreciated.

I wanted to move most functionality of my sketch into different classes, to keep things better organized. One class worked just fine, but when I had to use multiple classes and pass information between then things stopped working.

I have two classes, my main Remote class, and my RemoteDisplay class. The main class takes care of fx, measuring battery voltage and I want to display this through the RemoteDisplay class. To do so I initiate the Display class with a pointer to the Remote class, so I can access the voltage function. This, however, doesn't work, even though I forward declare the Remote class in the Display class.

Remote.ino

#include "Remote.h"

Remote Remote;

void setup() {
  Serial.begin(9600);
}

void loop() {
  Remote.display.showBattery();
}

Remote.h

#include "RemoteDisplay.h"
class Remote
{

public: 

  RemoteDisplay display;

  Remote( void );

  float voltage;

};

Remote.cpp

#include "Remote.h"

Remote::Remote(void)
{
  display.initiate(this);
}

RemoteDisplay.h

class Remote;

class RemoteDisplay
{

public:

  RemoteDisplay(void);

  void initiate( Remote * _pointer );

  void showBattery( void );

private:

  /* Reference to the remote class */
  Remote * pointer;
};

RemoteDisplay.cpp

#include "RemoteDisplay.h"

void RemoteDisplay::initiate( Remote * _pointer )
{
  pointer = _pointer;
}

void RemoteDisplay::showBattery( void )
{
  Serial.println( pointer->voltage );
}

I get the errors:

error: invalid use of incomplete type 'class Remote'
showBattery( pointer->voltage() );

note: forward declaration of 'class Remote'
class Remote;

Why is it so? Any help is much appreciated.

Source Link

Sketch with multiple classes (.h and .cpp) - how to interconnect classes

I wanted to move most functionality of my sketch into different classes, to keep things better organized. On class worked just fine, but when I had to use multiple classes and pass information between then things stopped working.

I have two classes, my main Remote class, and my RemoteDisplay class. The main class takes care of fx. measuring battery voltage and I want to display this through the RemoteDisplay class. To do so I initiate the Display class with a pointer to the Remote class, so I can access the voltage function. This, however, doesn't work, even though I forward declare the Remote class in the Display class.

Remote.ino

#include "Remote.h"

Remote Remote;

void setup() {
  Serial.begin(9600);
}

void loop() {
  Remote.display.showBattery();
}

Remote.h

#include "RemoteDisplay.h"
class Remote
{

public: 

  RemoteDisplay display;

  Remote( void );

  float voltage;

};

Remote.cpp

#include "Remote.h"

Remote::Remote(void)
{
  display.initiate(this);
}

RemoteDisplay.h

class Remote;

class RemoteDisplay
{

public:

  RemoteDisplay(void);

  void initiate( Remote * _pointer );

  void showBattery( void );

private:

  /* Reference to the remote class */
  Remote * pointer;
};

RemoteDisplay.cpp

#include "RemoteDisplay.h"

void RemoteDisplay::initiate( Remote * _pointer )
{
  pointer = _pointer;
}

void RemoteDisplay::showBattery( void )
{
  Serial.println( pointer->voltage );
}

I get the errors:

error: invalid use of incomplete type 'class Remote'
showBattery( pointer->voltage() );

note: forward declaration of 'class Remote'
class Remote;

Why is it so? Any help is much appreciated.