Undefined reference to a static member [duplicate]
Asked Answered
G

5

135

I'm using a cross compiler. My code is:

class WindowsTimer{
public:
  WindowsTimer(){
    _frequency.QuadPart = 0ull;
  } 
private:
  static LARGE_INTEGER _frequency;
};

I get the following error:

undefined reference to `WindowsTimer::_frequency'

I also tried to change it to

LARGE_INTEGER _frequency.QuadPart = 0ull;

or

static LARGE_INTEGER _frequency.QuadPart = 0ull;

but I'm still getting errors.

anyone knows why?

Grallatorial answered 2/2, 2012 at 10:12 Comment(3)
possible duplicate of undefined reference for static member, linker errorTungus
Where (if anywhere) have you defined WindowsTimer::_frequency?Statius
@CharlesBailey It my only member in the class.Grallatorial
A
159

You need to define _frequency in the .cpp file.

i.e.

LARGE_INTEGER WindowsTimer::_frequency;
Antonio answered 2/2, 2012 at 10:15 Comment(5)
Why should I do it so?Attu
@Attu Because C++ is not C#... a static data member is basically a global variable that just happens to be in the namespace of its class. The declaration inside the class definition is like an extern declaration of a global variable: It announces its existence, name and type, but does not create the object (because then, you would have an object in every file which includes the header with the class definition, the opposite of what's desired). Instead, there is exactly one definition of that object in one of the cpp files, and the linker will resolve code using that name to that object.Presto
@Attu So the reason you needed a separate object definition before C++17 is related to the antiquated build paradigm of C++: It uses the 1970s idea of "translation units" (files after peprocessing) that are compiled separately into object files with sparse information (e.g. no type information etc.). The link stage is language agnostic (e.g., knows nothing about C++) and separate from the compilation stage. (ctd.)Presto
(ctd) Environments like Java or C#, by contrast, produce more "semantically enriched" compilation artifacts (classes, packages), typically from several source files at once: Compilation and linking are not clearly separated which allows better interaction between code in different files.Presto
@Peter-ReinstateMonica I think your comment should be added in the answer.Spicer
S
78

With C++17, you can declare your variable inline, no need to define it in a cpp file any more.

inline static LARGE_INTEGER _frequency;
Suitcase answered 10/12, 2018 at 12:48 Comment(0)
F
44

Linker doesn't know where to allocate data for _frequency and you have to tell it manually. You can achieve this by simple adding this line: LARGE_INTEGER WindowsTimer::_frequency = 0; into one of your C++ sources.

More detailed explanation here

Francinefrancis answered 2/2, 2012 at 10:16 Comment(0)
P
30

If there is a static variable declared inside the class then you should define it in the cpp file like this

LARGE_INTEGER WindowsTimer::_frequency = 0;
Phillane answered 2/2, 2012 at 10:16 Comment(0)
N
4

This is a full code example for this other question which is indeed a duplicate of this one.

#include <iostream>

#include <vector>
using namespace std;

class Car
{

public:
    static int b;                   // DECLARATION of a static member


    static char* x1(int x)
    {
        b = x;                      // The static member is used "not as a constant value"
                                    //  (it is said ODR used): definition required
        return (char*)"done";
    }

};

int Car::b;                         // DEFINITION of the static 

int main()
{
    char* ret = Car::x1(42);
    for (int x = 0; x < 4; x++)
    {
        cout << ret[x] << endl;
    }

    return 0;
}
Nipissing answered 30/6, 2021 at 13:5 Comment(2)
Thanks for ur answer. Can u please share a link where I can read more about static member functions (important) and static variables(I have seen this alot). That contains more detail and example of static function and <b>faking static constructor for a class</b>Saad
@Saad You can refer to this video link from The Cherno on YT.Handful

© 2022 - 2024 — McMap. All rights reserved.