I'm having troubles understanding the inner workings of classes and such.
I have a class called Attributes
that contains parameters. In my .ino, I can simply instantiate an object with Attributes greenBark(1, 2);
and everything is hunkey dorey.
Problem: Now I need to create a Attributes object in my other class, called Tree. I figured out if my constructor doesn't have parameters, I can create an object within another class just fine. However, my attributes class constructor has parameters. When I try to create an parameterized object within another class, the compiler tosses an error. How can I create an attributes object within my tree class? I'm sorry if I got some of these terms mixed up, I'm trying to keep up!
Here are my current files (made for this example:)
The .ino file
#include "attributes.h"
#include "tree.h"
Attributes greenBark(1, 2);
void setup()
{
Serial.begin(9600);
}
void loop()
{
greenBark.printAttributes();
}
attributes.cpp
#include "attributes.h"
Attributes::Attributes(int size, int texture)
{
_size = size;
_texture = texture;
}
void Attributes::printAttributes()
{
Serial.print("GreenBark Attributes: ");
Serial.print("Size = ");
Serial.print(_size);
Serial.print(" Texure = ");
Serial.print(_texture);
Serial.println();
}
attributes.h
#pragma once
#include <Arduino.h>
class Attributes
{
public:
Attributes(int size, int texture); // constructor
void printAttributes(); // method
private:
int _size = 0;
int _texture = 0;
};
tree.cpp
#include "attributes.h"
#include "tree.h"
Tree::Tree()
{
}
tree.h
#pragma once
#include <Arduino.h>
#include "attributes.h"
class Tree
{
public:
Tree(); // constructor
void makeTree(); // method
// I want to create an attributes class object in my current class, so naturally I'd try to
// create the object just like I did in my .ino sketch, but the below line doesn't work.
//Attributes redBark(1, 2);
private:
};
The error provided is this:
In file included from C:\Users\ajrob\Dropbox\Projects\Electronics\Arduino Sketchbook\ParameterizedConstructor\ParameterizedConstructor.ino:2:0:
C:\Users\ajrob\Dropbox\Projects\Electronics\Arduino Sketchbook\ParameterizedConstructor\tree.h:16:24: error: expected identifier before numeric constant
Attributes redBark(1, 2);
^
C:\Users\ajrob\Dropbox\Projects\Electronics\Arduino Sketchbook\ParameterizedConstructor\tree.h:16:24: error: expected ',' or '...' before numeric constant
In file included from C:\Users\ajrob\Dropbox\Projects\Electronics\Arduino Sketchbook\ParameterizedConstructor\tree.cpp:2:0:
C:\Users\ajrob\Dropbox\Projects\Electronics\Arduino Sketchbook\ParameterizedConstructor\tree.h:16:24: error: expected identifier before numeric constant
Attributes redBark(1, 2);
^
C:\Users\ajrob\Dropbox\Projects\Electronics\Arduino Sketchbook\ParameterizedConstructor\tree.h:16:24: error: expected ',' or '...' before numeric constant
exit status 1
Compilation error: expected identifier before numeric constant
Attributes redBark {1, 2};
and it did not throw an error ... I have no idea if it does what you are expecting ...Attributes redBark {1};
andAttributes redBark {1, 2, 3};
both throw an error, so it might be what you want