# Undefined reference

**URL:** <https://forum.arduino.cc/t/undefined-reference/472947>\
**Category:** Programming\
**Created:** [July 30, 2017, 8:26pm UTC](https://forum.arduino.cc/t/undefined-reference/472947 "2017-07-30T20:26:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![guraknugen](https://avatars.discourse-cdn.com/v4/letter/g/35a633/32.png) [@guraknugen](https://forum.arduino.cc/u/guraknugen)\
**Post date:** [July 30, 2017, 8:26pm UTC](https://forum.arduino.cc/t/undefined-reference/472947/1 "2017-07-30T20:26:34Z")

</div>

I got a strange compile error:

```cpp
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:

```cpp
#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:

```cpp
#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.

---

<div class="post-metadata">

**Author:** ![septillion](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@septillion](https://forum.arduino.cc/u/septillion)\
**Post date:** [July 30, 2017, 8:40pm UTC](https://forum.arduino.cc/t/undefined-reference/472947/2 "2017-07-30T20:40:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![MarkT](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@MarkT](https://forum.arduino.cc/u/MarkT)\
**Post date:** [July 30, 2017, 8:47pm UTC](https://forum.arduino.cc/t/undefined-reference/472947/3 "2017-07-30T20:47:15Z")

</div>

Add the implementation:

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

```

to the .cpp file

Change the declaration in the class to :

```cpp
        static const int AvailableLimits[];

```

---

<div class="post-metadata">

**Author:** ![guraknugen](https://avatars.discourse-cdn.com/v4/letter/g/35a633/32.png) [@guraknugen](https://forum.arduino.cc/u/guraknugen)\
**Post date:** [July 30, 2017, 8:58pm UTC](https://forum.arduino.cc/t/undefined-reference/472947/4 "2017-07-30T20:58:04Z")

</div>

> 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…

---

<div class="post-metadata">

**Author:** ![guraknugen](https://avatars.discourse-cdn.com/v4/letter/g/35a633/32.png) [@guraknugen](https://forum.arduino.cc/u/guraknugen)\
**Post date:** [July 30, 2017, 9:03pm UTC](https://forum.arduino.cc/t/undefined-reference/472947/5 "2017-07-30T21:03:16Z")

</div>

> MarkT:  
> Add the implementation:
> 
> ```arduino
> const int SpeedLimit::AvailableLimits[] ={5,25,30,40,45,60};
> 
> ```
> 
> to the .cpp file
> 
> Change the declaration in the class to :
> 
> ```arduino
>         static const int AvailableLimits[];
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![septillion](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@septillion](https://forum.arduino.cc/u/septillion)\
**Post date:** [July 30, 2017, 9:05pm UTC](https://forum.arduino.cc/t/undefined-reference/472947/6 "2017-07-30T21:05:44Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![guraknugen](https://avatars.discourse-cdn.com/v4/letter/g/35a633/32.png) [@guraknugen](https://forum.arduino.cc/u/guraknugen)\
**Post date:** [July 30, 2017, 9:17pm UTC](https://forum.arduino.cc/t/undefined-reference/472947/7 "2017-07-30T21:17:05Z")

</div>

> 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 🙂

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…

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 10:39pm UTC](https://forum.arduino.cc/t/undefined-reference/472947/8 "2021-05-05T22:39:43Z")

</div>


