1

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
Juraj
18.3k4 gold badges31 silver badges49 bronze badges
asked Feb 24, 2023 at 18:01
5
  • en.cppreference.com/w/cpp/language/constructor Commented Feb 24, 2023 at 18:56
  • @Juraj I read that page over a few times understanding what I could and I'm not seeing how it helps my situation Commented Feb 24, 2023 at 19:15
  • 1
    I don't understand much of it either, so I tried 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}; and Attributes redBark {1, 2, 3}; both throw an error, so it might be what you want Commented Feb 24, 2023 at 20:04
  • 1
    @AJ_Smoothie your post is not Arduino specific, and really is a general programming question ... it belongs at stackoverflow.com/questions Commented Feb 24, 2023 at 20:19
  • @jsotola I tried the braces in my actual code (the parameters are input pins and default pin states) and it worked out just great. Thank you Commented Feb 27, 2023 at 15:39

1 Answer 1

0

I see two options here:

You might follow jsotola's comment and initialize redBark with braces:

Attributes redBark{1, 2};

For some reason, the syntax with parentheses is not allowed when initializing an object in a class declaration, but the syntax with braces is OK.

The other option is to initialize redBark in the Tree's constructor (and not in the class declaration) using an initializer list:

// In the class declaration:
Attributes redBark;
// The constructor:
Tree::Tree() : redBark(1, 2) {}
answered Feb 24, 2023 at 20:10
1
  • Using the curly braces worked out just fine! Thank you! Commented Feb 27, 2023 at 15:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.