Undefined Reference to Static Variable

Undefined reference to static variable

You only declared A::i, need to define A::i before using it.

class A  
{
public:
static int i;
static void init(){
i = 1;
}
};

int A::i = 0;

int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
A::i = 0;
A::init();

return 0;
}

Also your init() function should return a value or set to void.

Undefined reference to declared C++ static member variable

In C++, static variables are essentially syntactic sugar around global variables. Just like global variables, they must be defined in exactly one source file, with:

int Test::nb;

and if you want to initialize it with a particular value,

int Test::nb = 5; // or some other expression

Need help in defining and calling(using) Static variable in ts, it is always undefined

Try again with follow code below:

export class myClass implements OnInit {
static counter:any = 0;
static onListItemClick(PackDef: PackDefinition): void {
this.counter++;
console.log(this.counter);
}

Undefined reference to initialized static member variable with make_shared

Since C++17 the first code should work correctly: a static constexpr class member variable is implicitly inline which means the compiler takes care of making sure a definition exists .

Prior to C++17 the code has undefined behaviour (no diagnostic required) due to ODR violation. A static class member that is odr-used must also have an out-of-line definition.

Binding a reference to a variable counts as odr-use, and that happens with make_shared<int>(c) since that function is defined as :

template< class T, class... Args >
shared_ptr<T> make_shared( Args&&... args );

so the argument is bound to a reference parameter.


In theory you should be able to work around it with make_shared<int>(+c) ... then the reference is bound to the temporary result of +c and not to c itself, therefore there is no odr-use. Similar theory to your posted workaround in the question.

enum { c = 0 }; is another possible workaround, if the type is int in the real code .

QT Static library undefined reference to static variable

In C++, static member variables are essentially global exported "class variables" (so very different from other static variables, in a sense even opposite, you make file scope variables static to avoid them being exported globals).

And in that .h file, you only have variable declaration: you declare that this kind of variable exists, somewhere.

However, to actually make it exist for real, you have to define it. Therefore you need to add this to one .cpp file:

QString Library::name = QStringLiteral("initial value");

Additionally, it is class variable, so you (probably) shouldn't change it every time an instance is created, so your consturctor would be just:

Library::Library() {
}

If you want to initialize it from somewhere else (quite probably main(), to replace the code in your constructor, simply assign to it:

Library::name = whatever;

However, if you actually want to have it as instance variable (each instance/object of the class has its own copy), then just remove static from the definition in the .h file.

Also, with global variables (including static class variables), you have to be careful about initialization order. They're also global variables, with all the trouble that can bring. So, if you don't really need them, don't use them.



Related Topics



Leave a reply



Submit