0

みんな!以下のようなコード スニペットがあります: testcase.cpp

#include <string>
#include <iostream>
using namespace std;
class Test {
public:
    static int b ;
    static void test()
    {

        b = 3;
         cout << b<<endl;
    }
};
int main()
{
Test::test();

    return 0;
}

「ビルド」ボタンをクリックすると、出力メッセージは

エラー LNK2001: 未解決の外部シンボル "public: static int Test::b" (?b@Test@@2HA) 1>B:\PROGRAMPROJECT\visual 2015 pro\testcase\testcase\x64\Debug\testcase.exe : 致命的なエラーLNK1120: 1 つの未解決の外観

しかし、コードの場所を次のように変更すると:

  #include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
class Test {
public:
    //static int b;
    static void test()
    {
        static int b;//only change this line
        b = 3;
        cout << b << endl;
    }
};
int main()
{
    Test::test();

    return 0;
}

それはうまくいきます!理由がわかりません。誰か助けてください。私のIDEはvs pro 2015 + windows10です。

4

3 に答える 3