find the error in the following code:
class A { public: static int i; void print() { cout<< i << endl ; } }; int main() { A a; a.print(); }
I run the code above and I get the “undefined” link to “A :: I.” Why am I getting this error?
Since A::i is a member of static , it must be defined outside the class :
A::i
static
using namespace std; class A { public: static int i; // A::i is declared here. void print() { cout << i << endl; } }; int A::i = 42; // A::i is defined here.
static int i in class A is just a declaration, you need to define it outside the class by adding the operator int A::i = 0;
static int i
class A
int A::i = 0;
Source: https://habr.com/ru/post/1333550/More articles:How to disable Javadoc test report generation in Maven 3 site plugin? - javaDouble execution on one click jquery - javascriptwhy C # does not inherit the constructor from the base class - inheritanceHow to define an array without a fixed size? - arraysHow to find the control inside ItemsPanelTemplate in wpf? - c #Appdomain performance benefits versus process? - c #2D Graphics with Direct3D - directxC ++ Compiler Template Error Information - Tool for decoding error information - c ++How to optimize "class name" in Android XML layout? - androidGUI Templates for CRUD Web Applications - user-interfaceAll Articles