lundi 13 avril 2020

C++11 undefined reference to protected static member

What is wrong with this? Please reference the standard so people will think you have an inkling of what you're talking about and are not just another copy/paste time waster.

test.h:

#ifndef TEST_H 
#define TEST_H

class TheClass
{
protected:

    typedef struct {
        int id0;
        int id1;
    } a_tuple;

    static constexpr a_tuple list[] = {
        { .id0 = 0, .id1 = 3 },
        { .id0 = 1, .id1 = 4 },
        { .id0 = 2, .id1 = 5 }
    };

    static const a_tuple * find ( int id );

};

#endif // TEST_H

testp.cpp:

#include "test.h"

// This of course won't work
// constexpr TheClass::a_tuple list[] = {0};

const TheClass::a_tuple * TheClass::find ( int id )
{
    return &list[id];
}


int main()
{
    return 0;
}

Output:

> g++ test.cpp 
/usr/lib64/gcc/x86_64-suse-linux/9/../../../../x86_64-suse-linux/bin/ld: /tmp/ccaXvJM9.o: in function `TheClass::find(int)':
test.cpp:(.text+0x12): undefined reference to `TheClass::list'
collect2: error: ld returned 1 exit status

(I've been told to post here, so here you are. I personally don't give a toss about your replies. Cheerio)

Aucun commentaire:

Enregistrer un commentaire