0

I'm having difficulty using a class object in another class. I'm in the Arduino libraries and have the following simplified code

eh.h

#include <SoftwareSerial.h>

class EH_Serial_class : public Stream
{
private:
  uint8_t _mode;
  SoftwareSerial *_soft;  // SoftwareSerial is from SoftwareSerial.h

public:
  EH_Serial_class();
  ~EH_Serial_class();
  void config_soft(SoftwareSerial *soft);
}

eh.cpp

#include "eh.h"
EH_Serial_class::EH_Serial_class()
{
  _mode = 0;
  _soft = 0;
}

EH_Serial_class::~EH_Serial_class()
{
}

void EH_Serial_class::config_soft(SoftwareSerial *soft){
  _mode = EH_SOFT_SERIAL;
  _soft = soft;
}

I get the error that error: ‘SoftwareSerial’ has not been declared on the first declaration of SoftwareSerial

Can anyone help?

Edit: After following Jeffery's advice, I encountered a new error.

eh.h

//#include <SoftwareSerial.h>
class SoftwareSerial;

class EH_Serial_class : public Stream
{
private:
  uint8_t _mode;
  SoftwareSerial *_soft;  // SoftwareSerial is from SoftwareSerial.h

public:
  EH_Serial_class();
  ~EH_Serial_class();
  void config_soft(SoftwareSerial *soft);
  virtual size_t write(uint8_t byte);
}

eh.cpp

#include <SoftwareSerial.h> // tried after "#include eh.h" as well
#include "eh.h"

EH_Serial_class::EH_Serial_class()
{
  _mode = 0;
  _soft = 0;
}

EH_Serial_class::~EH_Serial_class()
{
}

void EH_Serial_class::config_soft(SoftwareSerial *soft){
  _mode = EH_SOFT_SERIAL;
  _soft = soft;
}

size_t EH_Serial_class::write(uint8_t byte){
  switch(_mode){
  case EH_STD_SERIAL:
    return Serial.write(byte);
  case EH_SOFT_SERIAL:
    return _soft->write(byte);
  default:
    return 0;
  }
  return 0;
}

I'm getting the error:

error: invalid use of incomplete type ‘struct SoftwareSerial’
error: forward declaration of ‘struct SoftwareSerial’

I think I'm using pointers right... what am I doing wrong now?

1
  • You likely have a circular dependency with those headers, but you didn't show us SoftwareSerial.h so we can't know... Commented Dec 9, 2013 at 20:28

1 Answer 1

1

Remove this line:

#include <SoftwareSerial.h>

and add this one at the beginning:

class SoftwareSerial;

The above is an incomplete type declaration that is enough for the compiler to work with your class pointer (since you are not performing any other action on the pointer, except assigning it).

Sign up to request clarification or add additional context in comments.

9 Comments

Changing that gives "error: SoftwareSerial.h: No such file or directory" -- in the Arduino library, SoftwareSerial is part of the std header files. So you don't see any other error in my code though???
@LightnessRacesinOrbit, yes, sir
Ok, that got it working in the header file -- but now it fails in the cpp file. Can you help?
@GarrettLinux, don't include #include <SoftwareSerial.h> in the cpp file.
I get the same error... I don't understand this. Why isn't including the .h file where SoftwareSerial is declared enough? I think I'm missing some kind of cpp syntax, like "using" or something.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.