Why do I get the compiler error '?

Good afternoon. The compilation runs fine, but when linking error

/tmp/cc0qoW0L.o: In function `main':
/tmp/cc4Zebx3.o: In function `Script::Script(std::basic_string, std::allocator >)':
A2.cpp:(.text._ZN16SkriptC2ESs[_ZN16SkriptC5ESs]+0x36): undefined reference to `Skript::counter'


class Script {

public:
 static int counter; 
 Skript (string v) : text(v) { 
counter++;
}
 ~Script() {
 counter--; }


class RSA : public Script{
public:


 RSA() : Skript("RSA"){ };
 ~RSA() { };

int main() 
{ 
Skript *v1 = new RSA(); // Error
 // int c1 = Script::counter;


//delete v1;
July 4th 19 at 23:53
2 answers
July 4th 19 at 23:55
Solution
super. thanks for the link !! - gay0 commented on July 4th 19 at 23:58
July 4th 19 at 23:57
Wrong here is not the compiler and linker. This question is from the times of si and comes from the technology compile: several compilation units are compiled independently, and then assembled together by the linker (a linker).

A static class member is a global variable, and if you declare it in each compilation unit, which the linker then take? Here we come to the conclusion: static int counter; in the header — is the same as extern. In other words, we do not declare a variable, and say: don't worry, the compiler in some compilation unit it will be.

And this issue is solved in the same way as for any extern’in one compilation unit (. CPP) declared
int Script::counter;
You can also statically initialize this counter:
int Script::counter = 0;

Oh yeah. And the technology of the compilation units where? From the assembler. If the program we have almost all the memory in 16 kilobyte, Assembly code is much, much higher than the PC memory. So we have to assemble the parts and then assemble the pieces of semi-native code in a complete program. Finally, to earned, Cyrus had to write only a C compiler and linker available.

Find more questions by tags C++