I am simply trying to use a class with attributes I can write to, no complex functions, constructors, destructors,etc.
I have the library I made added into Arduino (I'm pretty sure I added it in correctly) but still no luck. I get the error:
sketch_jul04a.ino:5:1: error: 'FC1' does not name a type (see the code pasted below for ref)
the type is "Container" and if I made the class incorrectly, why didn't line 1 or 3 throw an error?
Code
#include <MyContainer.h>
Container FC1;
FC1.RelayPin = 5;
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
Header File I Used
#ifndef MyContainer_H
#define MyContainer_H
#if ARDUINO < 100
#include <WProgram.h>
#else
#include <Arduino.h>
#endif
#include <string.h>
class Container {
public:
//Container();
// ~Container();
byte EchoPin; // Pin # of the Echo pin of the sensor
byte TrigPin; // Pin # of the Trigger pin of the sensor
byte RelayPin; // Pin # of the Pump Control Relay's input
byte LVL_HI; // Sensor reading when container is considered "Full"
byte LVL_LO; // Sensor reading when container is considered "Empty"
int MaxVolume; // Volume of container while "Full"
float PctFull; // % Full the Container is a.k.a. (LVL_HI/sesor_feedback)*100
String MainPump; // Pump inside the container (if applicaple)
String SupplyPump; // Pump Supplying the container (if applicable)
};
#endif
Any idea on what my problem might be or just a quick tip on how to implement my "Container" class in Arduino?
Two problems I can see.
Your class has no constructor.
You should probably use #include "MyContainer.h" instead of #include <MyContainer.h>
[ I agree, the rules for those are incomprehensible at the best of times, and Arduino's odd implementation makes it worse ].
Where did you put this header file ?
Try putting the file in the same directory where your sketch is.
Or, in the "libraries" directory.
@michinyon
Adding a constructor and destructor didn't help (assuming I did it correctly, see .cpp code pasted below) and the library has already been added to arduino( I added a folder to the library directory containing the neccesary .cpp, .h , and keywords files) . It still gives me the same error. Any more Ideas or tips for a simple class?
My .CPP file
#include "MyContainer.h" //include the declaration for this class
//<>
Container::Container()
{
EchoPin = 255;
TrigPin = 255;
RelayPin = 255;
LVL_HI = 1;
LVL_LO = 1;
MaxVolume = 1;
PctFull = 234.56;
MainPump = "Not_Assigned";
SupplyPump = "Not_Assigned";
}
//<>
Container::~Container(){/nothing to destruct/}
Please edit your post, select the code, and put it between [code] ... [/code] tags.
You can do that by hitting the "Code" icon above the posting area. It is the first icon, with the symbol: </>
How to use this forum
Your code looks like it "should" work.
I would suggest putting the declaration of the class straight into your arduino sketch file, preceding the setup( ) and preceding the declaration of the instance of the class.
If that works, then you have some kind of problem with the #include process not working for you. If that doesn't work, you might get a better error message.
That "does not name a type" error message usually means a problem with the includes. It seems strange, sometimes you get an error with " can't find .h file " and sometimes you don't.
The other possibility, is that the name "Container" conflicts with something else. Try changing it to something else, see if it makes a difference.
And get rid of that wprogram.h stuff. No one is going back to 2010.
When I try compiling your code, the answer suddenly becomes obvious.
The line being objected to, is not the line where you declare the container instance FC1.
It is the line where you try to assign 5 to the member of the class. That's an ordinary piece of code and IT HAS TO BE INSIDE A FUNCTION.
Move that line into setup( ), and it works.
@michinyon
Moving it into setup works!!! Thanks for helping me out. I suppose a more permanent solution would be to make each of the attributes linked to a function in my .cpp file, but at least I got past this hurtle.
michinyon:
Your class has no constructor.
In C++, if a class has no constructor, the compiler will auto generate one. It will be the equivalent of:
Container::Container()
{
}
As you can see, it does nothing, but the code should compile fine without it.