mardi 28 novembre 2023

Why is a reference to a static variable unresolved in the linker? [duplicate]

Win11
MSVC 2022 (64-bit) - Version 17.8.0
C++14

I have a simple program, below. I can not reference the static variables within a function in the source code (*.cpp file), but I can reference object variables. I get a linker error saying that references to the static variables are unresolved, and the message lists the static variable. If I move the code to the header file then the compilation works.

Another issue (not shown) is I can not reference these static variable from another object. It appears the only reference possible is by using setter and getter functions in the header. Is this part of the C++14 standard?

I don't understand how to reference static variables?

Linker Error: (there is one error for highband and lowband)

Error LNK2001 unresolved external symbol "private: static double Test::highband" (?highband@Test@@0NA) Test D:\home\skidmarks\Projects\Test\Test\Test\Test.obj 1

Code:
// header: Test.h
    class Test  {
       static double  delta;
       static double  highband;
       double lowband;
       void common_setup();
    }; // class Test

 // source: Test.cpp
    # include "Test.h"
    void Test::common_setup() {
       Test::delta = 1.0;
       highband = 1.0;
       lowband = 1.0;
    };

    int main() {}

Aucun commentaire:

Enregistrer un commentaire