Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unresolved external with static variables

I have a class called Pub which has the following header:

#pragma once



class Pub
{

public:
    static double X_FACTOR;
    static double Y_FACTOR;
    static const int INIT_SCREEN_WIDTH=500;
    static const int INIT_SCREEN_HEIGHT=550;


    Pub(void);
    ~Pub(void);
};

I am trying to set the variable Y_FACTOR in main.cpp with the following:

Pub::Y_FACTOR=1.0;

and yes Pub.h is included properly which can be demonstrated as I can access INIT_SCREEN_WIDTH and INIT_SCREEN_HEIGHT However when I do this I get the following error:

Error 6 error LNK2001: unresolved external symbol "public: static double Pub::Y_FACTOR" (?Y_FACTOR@Pub@@2NA) C:\Users\Pedro-Estevan-Juarez\Documents\Visual Studio 2012\Projects\Project2\Project2\main.obj Project2 Error 7 error LNK1120: 1 unresolved externals C:\Users\Pedro-Estevan-Juarez\Documents\Visual Studio 2012\Projects\Project2\Debug\Project2.exe 1 1 Project2

I suspect this is something syntax wise, can someone please help me with this?

like image 621
user2853108 Avatar asked Dec 14 '25 03:12

user2853108


1 Answers

The code inside the class definition is just a declaration. You need to add definition of the static variable in a cpp file. Add this in your cpp file and in file scope before any function using it.

double Pub::Y_FACTOR;
like image 61
ZijingWu Avatar answered Dec 15 '25 16:12

ZijingWu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!