How to solve the problem of unparsed external symbols in CPP?

CPP rookie, compile hint according to the code typed in the book example:

 "private: static int value::total_length" (?total_length@value@@0HA)
Project1    C:\Users\Kellen\source\repos\Project1\Project1\ex3_10.obj    1

the source code is as follows:

-sharpinclude <iostream>
-sharpinclude <string>
using namespace std;
class value {
    static int total_length;
    int length;
    char *contents;
public:
    value(const char *s) {
        length = strlen(s);
        contents = new char[length + 1];
        strcpy(contents, s);
    }

    static int set_total_length(value &obj) {
        total_length += obj.length;
        return total_length;
    }

    ~value() {
        delete[]contents;
    }
};

int main(void) {
    value obj1("the first object");
    cout << obj1.set_total_length(obj1) << endl;
    value obj2("the second object");
    cout << obj2.set_total_length(obj2) << endl;
}

Baidu is fruitless. I hope some friends can help me solve my problem, or tell me how it works. Thank you very much!

CPP
Aug.08,2021

this tutorial probably teaches you to use static members. See if you left out the sentence int value::total_length = 0 ; or if the textbook example is meant to examine your question, you should go through the static members section of the textbook again.

the problem is that static members must be initialized before they are used, otherwise the link will go wrong

Menu