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?
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With