Thread: Use Class in own Class / constructors

  1. #1
    Registered User
    Join Date
    Sep 2022
    Posts
    8

    Use Class in own Class / constructors

    Greetings,

    I am an OOP beginner and have the following question:

    I have my own class and want to use another class (movingAvgFloat.h) to calculate a moving average.
    (GitHub - ThingEngineer/movingAvgFloat: A simple Arduino library for calculating floating point moving averages.)

    I already found out that you can't just declare the movingAvgFloat on top, but have to load it with constructors list, because the class is loaded with a parameter.
    That works so far. Now I want to use a second object (Avg2) of the movingAvgFloat class, which I can't manage.

    My code:[CODE]

    Code:
          #include <movingAvgFloat.h>
          
          class myClass
          {
          
          private:
              movingAvgFloat Avg1;
              
          public:
              myClass() : Avg1(6) {}; // Avg with 6 cells
          ...
    What is the syntax for using two objects, something like: ?

    Code:
          private:
              movingAvgFloat Avg1;
              movingAvgFloat Avg2;
    
    public:
              myClass() : Avg1(6) {}, Avg2(6) {} // does not work;)
    Thx a lot

    michael

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You only need one pair of braces for the empty code.
    Code:
        myClass() : Avg1(6), Avg2(6) {}; // Avg with 6 cells each
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2022
    Posts
    8
    Thanks a lot, I will try it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class has no constructors
    By 843 in forum C++ Programming
    Replies: 7
    Last Post: 04-18-2011, 12:11 PM
  2. Class constructors
    By c++.prog.newbie in forum C++ Programming
    Replies: 2
    Last Post: 06-21-2006, 09:15 PM
  3. Class Constructors
    By d_heyzie in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2006, 04:26 AM
  4. Replies: 4
    Last Post: 12-29-2002, 12:29 AM
  5. help with class and constructors
    By stautze in forum C++ Programming
    Replies: 14
    Last Post: 10-16-2002, 07:56 PM

Tags for this Thread