It is very important that my function is static, I need to access and modify another static/non-static class member in order to print it out later. How can I do that?
Flow
- Class is initiated
- Constructor sets variable to something using internal function that must be static
- Some time later I print that variable
Example code
#include <iostream>
class MyClass
{
public:
static int s;
static void set()
{
MyClass::s = 5;
}
int get()
{
return MyClass::s;
}
MyClass()
{
this->set();
}
};
void main()
{
auto a = new MyClass();
a->set(); // Error
std::cout << a->get() << std::endl; // Error
system("pause");
}
Error
LNK2001: unresolved external symbol "public: static int MyClass::s" (?s@MyClass@@2HA)
LNK1120: 1 unresolved externals