Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: Compiler Error - Own Audio Library class

  1. #1
    Junior Member
    Join Date
    May 2020
    Posts
    4

    Compiler Error - Own Audio Library class

    Hey,

    I am trying to program my own class for the audio library to practice some basics of realtime audio synthesis. For this I had a look at the guidelines for creating your own classes on the Teensy website and used the object template displayed there (just changing the name of the class to "Synth"):

    Code:
      #include "core_pins.h"
      #include "AudioStream.h"
      #include "Arduino.h"
    
    class Synth : public AudioStream
    {
    public:
            Synth() : AudioStream(1, inputQueueArray);
            virtual void update(void);
    private:
            audio_block_t *inputQueueArray[1];
    };
    When I tried to compile the code I unfortunately got several compiler errors. I got rid of the "F_CPU_ACTUAL" was not declared in this scope by browsing the forum and including "core_pins.h" as described in this thread.
    However I'm still facing one compiler error I just can't get behind:

    [{
    "owner": "cpp",
    "severity": 8,
    "message": "expected '{' at end of input",
    "startLineNumber": 8,
    "startColumn": 49,
    "endLineNumber": 8,
    "endColumn": 49
    }]

    Has anyone an idea on how to solve this? Appreciate your help!

    Cheers, Matthias

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    27,085
    Opps, well that's embarrassing! That web page has shown slightly wrong syntax all these years. I've updated it just now.

    In C++ the init list syntax can only be used where the constructor is defined (actual code), not where it's merely declared. So if the class is declared in a .h file and the constructor code is not inline in the class declaration, then the initialization list would need to be in the definition with the actual code (presumably the .cpp file).

    Normally with the audio library, the constructors for non-hardware classes are both declared+defined with inline code, because they usually don't do much other than initialize member variables. So normally it would look like this in the .h file:

    Code:
    #include <Arduino.h>
    #include <AudioStream.h>
    
    class AudioEffectTemplate : public AudioStream
    {
    public:
            AudioEffectTemplate() : AudioStream(1, inputQueueArray) {
              // any extra initialization
            }
            virtual void update(void);
    private:
            audio_block_t *inputQueueArray[1];
    };
    But if you wanted to only declare stuff in the .h file (no actual code) and define (actual code) it in the .cpp file, the you would use this in the .h file:

    Code:
    class AudioEffectTemplate : public AudioStream
    {
    public:
            AudioEffectTemplate();
            virtual void update(void);
    private:
            audio_block_t *inputQueueArray[1];
    };
    and of course you would put the constructor's actual code (even if only an empty function) in the .cpp file, like this:

    Code:
    AudioEffectTemplate::AudioEffectTemplate() : AudioStream(1, inputQueueArray)
    {
            // any extra initialization (likely more substantial if put into the .cpp file)
    }

  3. #3
    Junior Member
    Join Date
    May 2020
    Posts
    4
    oooh yeah! Thank you very much for the fast reply, now everything works just fine!
    Yes, my constructor code is not inline in the class declaration but in the .cpp file just as you assumed. I already thought that I did something wrong with the declaration and definition of the constructor, but I didn't come up with the solution on my own so your explanation is very much appreciated!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •