Help with Coding

BriComp

Well-known member
I am a relative newby to c++ and I could do with some help.
I am trying to pass a type from a header file to an ino but it's not compiling.
I have generated a cut down example to demonstrate the problem.
I have searched the web for help but nothing has.
Here is the cut down code:-
TestHeader.h
Code:
#pragma once
#include "Arduino.h"

class TestHeader {

public:

	enum dirType {
		forward,
		backward
	};


	struct testType {
		uint32_t aa;
		uint32_t bb;
		uint32_t cc;
		uint32_t dd;
		dirType   ee;
		uint32_t ff;
		uint32_t gg;
		bool	     hh;
	};

	testType SaveData();

private:

	uint32_t a;
	uint32_t b;
	uint32_t c;
	uint32_t d;
	dirType   e;
	uint32_t f;
	uint32_t g;
	bool	     h;
};
TestHeader.cpp
Code:
#include "Arduino.h"

#include <TestHeader.h>

TestHeader::testType TestHeader::SaveData() {
	testType tst;

	tst.aa = a;
	tst.bb = b;
	tst.cc = c;
	tst.dd = d;
	tst.ee = e;
	tst.ff =   f;
	tst.gg = g;
	tst.hh = h;

	return tst;
}
Test.ino
#include <Arduino.h>
#include <TestHeader.h>

TestType testData;

void setup()
{
testData = SaveData{};

}

// Add the main program code into the continuous loop() function
void loop()
{


}
 
You declared your struct "testType" nested in the class TestHeader. This is OK but probably not what you want.

If you want to access this type you need to fully qualify it. E.g. in Test.ino you need to write

Code:
TestHeader::testType testData;

Then, in setup you write
Code:
testData = SaveData{}

This makes no sense. SaveData is a member function of the TestHeader class. To call it you need an instance of this class. E.g.:

Code:
#include "Arduino.h"
#include "TestHeader.h"

TestHeader::testType testData;

TestHeader th;   //create an instance of TestHeader


void setup()
{
    testData = th.SaveData();
}

// Add the main program code into the continuous loop() function
void loop()
{
}

This compiles, but all in all it does not make much sense to me. Might be easier to help if you can explain what you want to achieve with this construction?
 
Yes in my quick scribbling I had forgotten to add "TestHeader th; //create an instance of TestHeader"

In am sure I had used "TestHeader::testType testData;" in all my attempts to get the original code working.
But putting together your suggestions makes it work just fine. Thank you.

Now to incorporate it into the original project.:)
 
Back
Top