Breaking project into files: Confusion with Instantiated objects..

I have been breaking my project into .cpp and .h files. I am a bit confused about instances of objects.

For Instance (pun intended), If I wish to use the SoftwareSerial library in both my .ino main sketch and one of the .cpp files can I share one instance of the SoftwareSerial object? My head says yes, but I can't work out how to do it. I always end up with 'multiple definition of xyz' or ' was not declared in this scope' type errors.

**Example ** 1 instance of the SoftwareSerial object instantiated in hello.ino

// hello.ino

//INCLUDES
#include <SoftwareSerial.h>
#include "Arduino.h"
#include "world.h"

//DEFINES
//      VARIABLE		|AR. NUM.  |PIN NAME	|IC PIN	|FUNCTION
//-----------------------------------------------------------------------------------------------------------------------	
#define Soft_TXD        14          //PA1		17		Software Serial TXD
#define Soft_RXD        15          //PA2		18		Software Serial RXD


// INSTANTIATIONS
SoftwareSerial SoftSerial(Soft_RXD, Soft_TXD);    //RX, TX


void setup() {

	Serial.begin(9600);		// Software Serial start	
}

void loop()
{
	SoftSerial.print("Hello ");		// Print to softwareSerial
	world.Print("world!");			// Send to world class to print to SoftwareSerial
}
// world.h
#ifndef _WORLD_h
#define _WORLD_h

class worldclass
{
public:
	worldclass();
	void Print(String);
};
extern worldclass world;
#endif
// world.cpp
#include "Arduino.h"
#include "world.h"


worldclass::worldclass() {}


void worldclass::Print(String) {
	
	SoftSerial.print("world");
}



////////////////////////////////
worldclass world = worldclass();

RESULTS (which is to be expected)

world.cpp: 12:2: error: 'SoftSerial' was not declared in this scope
   SoftSerial.print("world")

I can 'fix' it by creating another instance of the SoftwareSerial in the world.cpp file but then, as far as I understand it, I will have two instances in memory. That just seems wrong and I feel I should be able to use just one.

I think it may be something to do with using Extern in the .cpp file but I can not seem to get it to work with an object instance, only variables.

Pass a reference to the SoftwareSerial object (as a Stream &) into your class's constructor:

class worldclass {
  public:
    worldclass(Stream &str);
    void Print(String s);

  private:
    Stream &printStream;
};

worldclass::worldclass(Stream &str): printStream(str) {
}

void worldclass::Print(String s){
  printStream.print(s);
}

An example with a pointer to the class. The instance is created / deleted dynamically.

// hello.ino

#include "world.h"

Worldclass * world ;

void setup() {
  Serial.begin(115200);   // Software Serial start  
}

void loop()
{
  world = new Worldclass () ;
  Serial.print("Hello ");   // Print to Serial
  world->Print("world!");      // Send to world class to print to SoftwareSerial
  Serial.println () ;
  delete world ;
}
// world.h
#ifndef _WORLD_h
#define _WORLD_h

class Worldclass
{
public:
	Worldclass();
	void Print(String);
};
#endif
// world.cpp
#include "Arduino.h"
#include "world.h"


Worldclass::Worldclass() {}

void Worldclass::Print(String) {
	Serial.print("world");
}

Thanks guys I'll give the suggestions a go when I have finished creating the rest of my latest new .cpp class.

You will want to come to a very clear understanding of what the following terms mean: define, declare, instantiate, initialize and assign along with storage classes, auto, static, extern and register and type qualifiers, const, volatile, restrict and _Atomic. It's too much to include in a response, so I'm giving you some google homework. I will be honest with you, it's not intuitive. H files typically include declarations, but not definitions, while C, CPP and INO files typically include definitions. These terms can have subtle differences between variables and functions and classes.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.