Error in " JUCE Tutorial: Create MIDI data " example code?

As someone just getting started with JUCE, I’ve been going through many of the tutorials JUCE provides on their website. However, I’ve arrived at a bit of a headscratcher while trying to follow along with the “Create MIDI data” tutorial.

At the very end, the tutorial instructs you to create a function with a range-based for loop like so:

 void timerCallback() override
    {
        auto currentTime = juce::Time::getMillisecondCounterHiRes() * 0.001 - startTime;
        auto currentSampleNumber = (int) (currentTime * sampleRate);     

        for (const auto metadata : midiBuffer)                           
        {
            if (metadata.samplePosition > currentSampleNumber)           
                break;

            auto message = metadata.getMessage();
            message.setTimeStamp (metadata.samplePosition / sampleRate);
            addMessageToList (message);
        }

        midiBuffer.clear (previousSampleNumber, currentSampleNumber - previousSampleNumber);
        previousSampleNumber = currentSampleNumber;                                          
    }

The problem I am having is with this bit:

for (const auto metadata : midiBuffer) {...}

Visual Studio gives w̶a̶r̶n̶i̶n̶g̶ an error underscoring the midiBuffer that says: this range-based ‘for’ statement requires a suitable “begin” function and none was found

I’ve tested both my own code which I copied from the tutorial as well as the files with the completed code provided by the tutorial itself, yet the same error persists. Is this an error with the tutorial’s code, or is there something else going on that I’m missing?

By warning you mean an error that doesn’t allow you to compile the example, or just a warning and it compiles and executes fine? Because warnings and errors are different things.

This code should be okay, you can take a look at range-for loops to check but I haven’t compiled this example to try.

Sorry, I meant an error that doesn’t allow it to compile.

Which version of JUCE are you using? The range based iterators for MidiBuffer are new in JUCE 6. You can either upgrade, or work out how to use the old MidiBuffer::Iterator, which has a different interface.

I’m using the latest version of JUCE ( JUCE 6.0.2 ) along with the latest version of Visual Studio Community (ver. 16.7.6)

Still, the error persists.

I’m wondering if maybe a line of code got omitted from the source files or something? Maybe there are settings on my personal computer that are preventing a library from being accessed? I really don’t know.

What I’d really like to know is if others are able to run the source code provided by the tutorial in Visual Studio. I think that would narrow the problem down to it being rooted on my end or the tutorial’s end.

just to double check, can you show your definition for this?

Coming straight from the tutorial source code, midiBuffer is defined as follows:

class MainContentComponent   : public juce::Component,
                               private juce::Timer
{
 public:
   //...
 private:
   //...
   void timerCallback() {...}
   //...
   juce::MidiBuffer midiBuffer;
};

And you have not accidentally defined another one someplace? as a local variable, or a parameter into that function?

If I drop your code directly into any of my source files (and define the few things needed) it compiles just fine… something must wrong with some config somewhere…

Hmm… no it hasn’t been defined anywhere else. If it works on your end then maybe something is wrong on my end.

If its not asking too much, would you be willing to download the ZIP file provided by the tutorial and try running that code? (FYI the complete code is in MidiMessageTutorial_02.h)

As a beginner I would really like to know if the exact code provided by the tutorial is functional, as I still don’t know how to “define the few things needed”. (which is why I’m doing the tutorials haha)

It compiles and runs like a champ. Something is wrong with your setup. I did have to toggle the ‘make local copy’ options, because that is how I work, but otherwise I just downloaded, opened in projucer, launched the ide from there, and ran

SOLVED:

Okay so just an update, I found out what was causing my problem. When I downloaded JUCE 6, I never updated the paths to the new JUCE folder in the Projucer. It turns out that even though I had JUCE 6 downloaded, I never replaced the old JUCE folder. So, in the end I was still telling the Projucer to use the outdated version of JUCE instead of the new one. I fixed it when I deleted the old JUCE folder and updated the correct paths to the right folders.

For any noobs like me: I did this this by going to FIle -> Global Paths in the Projucer and then selecting the new JUCE folder as well as the new JUCE modules folder.

1 Like