error: 'SoftwareSerial' does not name a type

Hello,

I',m trying to set the ports of the SoftwareSerial dynamically when starting a library (controlling an OLED).

After compiling the follwing code I get the message on the cpp file: OLED.h:15: error: 'SoftwareSerial' does not name a type. I've searched the formu and the net and tried several approaches. Can anyone give me a direction what I'm doing wrong?

Header file: OLED.h

// OLED.h

#ifndef OLED_h
#define OLED_h

#include "Arduino.h"
#include <SoftwareSerial.h>


class OLED
{
 public:
	void init(uint8_t intRx, uint8_t intTx);
 private:
	SoftwareSerial Serial_OLED;
};


#endif

.cpp file: OLED.cpp

// related topics:
// http://forum.arduino.cc/index.php/topic,114761.0.html
// http://forum.arduino.cc/index.php?topic=256604.0
// http://arduino.cc/en/Hacking/LibraryTutorial

#include "OLED.h"
#include <SoftwareSerial.h>

void OLED::init(uint8_t intRx, uint8_t intTx)
{
	SoftwareSerial Serial_OLED(intRx, intTx);

}

Include <SoftwareSerial.h> in you main project file *.ino

void OLED::init(uint8_t intRx, uint8_t intTx)
{
	SoftwareSerial Serial_OLED(intRx, intTx);

}

Consider what the scope of Serial_OLED is. Then, consider why this code is useless in the long run.

Thanks Shev and PaulS. I needed both comments to get me further in creating my first simple library in C++ :slight_smile: :). Here's the result.

.ino

#include <SoftwareSerial.h> // yes, this is needed here also
#include "OLED.h"

OLED iOLED;

void setup()
{
  iOLED.init(9600,12,13);
  iOLED.WriteString(2,2,"Test OK today",0); // write Test on position 2,2 with font type 0
}

void loop()
{
  /* add main program code here */
}

OLED.h

// OLED.h

#ifndef OLED_h
#define OLED_h

#include "Arduino.h"
#include <SoftwareSerial.h>

class OLED
{
 public:
	void init(uint32_t intBaud, uint8_t intRx, uint8_t intTx);
	void WriteString(uint8_t intX, uint8_t intY, const char *strWrite, uint8_t intFontNo);
 private:
	SoftwareSerial *_Serial_OLED; 
};

#endif

OLED.cpp

// related topics:
// code of the DigoleSerial.h for the OLED screen 128x64 (www.digole.com)
// http://forum.arduino.cc/index.php/topic,114761.0.html
// http://forum.arduino.cc/index.php?topic=256604.0
// http://stackoverflow.com/questions/19937415/using-softwareserial-as-a-private-member-variable
// http://arduino.cc/en/Hacking/LibraryTutorial
// http://www.tutorialspoint.com/cplusplus/cpp_classes_objects.htm

#include "OLED.h"

void OLED::init(uint32_t intBaud, uint8_t intRx, uint8_t intTx)
{
	_Serial_OLED = new SoftwareSerial(intRx, intTx);
    _Serial_OLED->begin(9600);
	_Serial_OLED->print("CL"); // clear screen
	delay(100); //  init
	_Serial_OLED->print("CS0"); // cursor (ON=CS1 / OFF=CS0)
	delay(100); //  init
}

void OLED::WriteString(uint8_t intX, uint8_t intY, const char *strWrite, uint8_t intFontNo) {
	//coordinates
	_Serial_OLED->print("TP");
	_Serial_OLED->write(intX);
	_Serial_OLED->write(intY);
	// font
	_Serial_OLED->print("SF");
	_Serial_OLED->write(intFontNo);
	//text
	_Serial_OLED->print("TT");
	_Serial_OLED->println(strWrite);
}