错误 LNK2019:未解析的外部符号 "public: __thiscall RNG::RNG(unsigned __int64)" (??0RNG@@QAE@_K@Z) 在函数 _main 中引用

error LNK2019: unresolved external symbol "public: __thiscall RNG::RNG(unsigned __int64)" (??0RNG@@QAE@_K@Z) referenced in function _main

当我使用 VSCode 中的 cl.exe 构建下面的文件时,我遇到了上述错误。 我盯着这段代码看了一整天,也看不出错误。我尝试了很多东西,有些不合理,但无济于事。

尝试构建它时出现错误:

"/调试"
"/out:D:\Apps_for_C++\PokerWorkspace\RNG\RNG.exe"
“RNG.obj”
“RNG.obj:错误 LNK2001:未解析的外部符号私有:静态无符号 __int64 RNG::Seed”
"?Seed@RNG@@0_KA)"
"D:\Apps_for_C++\PokerWorkspace\RNG\RNG.exe : 致命错误 LNK1120: 1 个未解决的外部事件"

最初我将主例程放在单独的文件夹“Main”中(请参阅任务文件)并得到相同的结果。我已将其附加到 RND.cpp 文件中。我还在一个单独的文件夹“Includes”中有 RNG.h 文件,但后来将它移到了 RNG 文件夹中。

如果我注释掉“RNG Random;”这一行我得到一个不同的错误:

"/调试"
"/out:D:\Apps_for_C++\PokerWorkspace\RNG\RNG.exe"
“RNG.obj”
“RNG.obj:错误 LNK2001:未解析的外部符号私有:静态无符号 __int64 RNG::Seed” “(?Seed@RNG@@0_KA)”
"D:\Apps_for_C++\PokerWorkspace\RNG\RNG.exe : 致命错误 LNK1120: 1 个未解决的外部事件"

如果我删除主例程,它编译正常,但链接器当然会失败,抱怨没有主例程。

它表现得好像没有看到 RNG.h 文件。

这让我抓狂 - 谁能告诉我我做错了什么??我可以尝试其他一些编译器和链接器,但我很固执,真的很想知道这种方法有什么问题。

//RNG.h (located in folder "${workspaceFolder}/RNG"

#ifndef RNG_H
#define RNG_H
    class RNG
    {
    private:
        static unsigned long long Seed;
 
    public:   
        RNG ();   // Default constructor;
        RNG (unsigned long long ullSeed); // Constructor with initial seed
        unsigned long long RNGGet();  //Get a random number
    };
#endif


//RNG.cpp (located in folder "${workspaceFolder}/RNG"

#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <RNG.h>

using namespace std;

const unsigned long long T1 = 60LL, T2 = 59LL;

RNG::RNG()   // Default constructor
{
    int i;

    Seed = 3;

    for (i=0; i<7*T1; i++)      //Step thru some numbers to stabilize
    {
        RNGGet();
    }
};

RNG::RNG(unsigned long long ullSeed) // Constructor with initial seed
{
    const unsigned long long Mask1 = powl(2LL,T1);
    const unsigned long long Mask = Mask1 - 2LL;
 
    int i;

    Seed = ullSeed % Mask;
    for (i=0; i<7*T1; i++)      //Step over trivial numbers
    {
        RNGGet();
    }
};

// This method is used to obtain a random number.  It returns an unsigned Long Long.
unsigned long long RNG::RNGGet()
{
    const long long Test1 = powl(2LL, T1-1LL);
    const long long Test2 = powl(2LL, T2-1LL);
    const unsigned long long Mask1 = powl(2LL,T1);
    const unsigned long long Mask = Mask1 - 2LL;
    const unsigned long long Taps = Test1 | Test2;
    const unsigned long long Scram = 0x693474786E579bd % Mask;

    unsigned long long Test;

    Test = Seed & Taps;
    Seed = (Seed << 1) & Mask;
    if (Test == Test1 | Test == Test2){
        Seed = Seed | 1LL;
    }
    return Seed * Scram;
};

int main()
{
    // Create an instance of RNG
    RNG Random;
}


// tasks.json (located in folder "${workspaceFolder}/.vscode")
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: cl.exe build active file",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/Fe:",
                "${fileDirname}\${fileBasenameNoExtension}.exe",
                "${file}",
                "-I${workspaceFolder}/Includes"
                "-I${workspaceFolder}/Main"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: cl.exe"
        }
    ]
}
...


您有需要在 class 之外定义的 Seed 静态变量。 unsigned long long Rng::Seed = some_value;

成功!在按照 sonulohani 的建议进行更正后,我发现我还有 #include 而不是 #include "RNG.h"。现在编译和链接没有错误。