Undefined reference

I got a strange compile error:

Arduino:1.6.8 Hourly Build 2015/12/23 04:34 (Linux), Card:"Arduino/Genuino Uno"

sketch/SpeedLimit.cpp.o: In function `SpeedLimit::ChangeLimit(int)':
sketch/SpeedLimit.cpp:18: undefined reference to `SpeedLimit::AvailableLimits'
sketch/SpeedLimit.cpp:18: undefined reference to `SpeedLimit::AvailableLimits'
collect2: error: ld returned 1 exit status
exit status 1
Compile error.

SpeedLimit.cpp looks like this:

#include "SpeedLimit.h"

SpeedLimit::SpeedLimit() {
    Index=DefaultIndex;
    Limit=AvailableLimits[Index];
}

int SpeedLimit::GetLimit() {
    return Limit;
}

void SpeedLimit::ChangeLimit(int Change) {
    Index+=Change;
    if(Index<StartIndex)
        Index=StartIndex;
    else if(Index>EndIndex)
        Index=EndIndex;
    Limit=AvailableLimits[Index];
 }

void SpeedLimit::PrintLimit() {
    
}

Here’s the SpeedLimit.h file:

#ifndef SpeedLimit_h
#define SpeedLimit_h

#include "Arduino.h"

class SpeedLimit {
    public:
        SpeedLimit();
        int GetLimit();
        void ChangeLimit(int);
        void PrintLimit();
    private:
        int Limit;
        int Index;
        static const int StartIndex=0;
        static const int DefaultIndex=4;
        static const int EndIndex=5;
        static constexpr int AvailableLimits[]={5,25,30,40,45,60};
};

#endif

The error occurs at SpeedLimit::ChangeLimit, but the funny thing is that exactly the same line can be found in SpeedLimit::SpeedLimit a few lines up, but the compiler seems to be okay with that, for some reason…

AvailableLimits is an array, and I had some troubles declaring it, but I don’t think that’s the problem this time (since I have had it there without any problems before).

This really confuses me, so all kinds of help is deeply appreciated.

Why do you use constexpr and not just const? As far as I know constexpr is only allowed on a variable since C++17 which is still a draft. Arduino IDE is set to compile for C++11.

Add the implementation:

const int SpeedLimit::AvailableLimits[] ={5,25,30,40,45,60};

to the .cpp file

Change the declaration in the class to :

        static const int AvailableLimits[];

septillion:
Why do you use constexpr and not just const? As far as I know constexpr is only allowed on a variable since C++17 which is still a draft. Arduino IDE is set to compile for C++11.

Well, I couldn't get it to work with const, so I did a web search and found something about constexpr and it worked… (and I didn't see anywhere that it's only allowed since C++17) but now I added some things and now I have this error instead…
Usually I don't use C++ much, but now I decided to give it a try, so I'm kind of learning by trial and error here…

MarkT:
Add the implementation:

const int SpeedLimit::AvailableLimits[] ={5,25,30,40,45,60};

to the .cpp file

Change the declaration in the class to :

        static const int AvailableLimits[];

Thanks, it worked.
Still I wonder though, why it didn't complain on the same line inside SpeedLimit::SpeedLimit().

Have you tried to compile that other part without the part with the error? It might be the compiler just had a big enough error it just stopped looking and stopped compiling right in it's tracks :slight_smile:

septillion:
Have you tried to compile that other part without the part with the error? It might be the compiler just had a big enough error it just stopped looking and stopped compiling right in it's tracks :slight_smile:

No, I didn't try that, I just assumed that it compiled all files from the top down, and therefore should find the first one first…

But it works now anyway. Well, it compiles without errors, at least. Only some parts of the complete program works (9 files), but that's another story. I'll dig into it tomorrow or so, now it's time to sleep…