Search code examples
c++visual-studio-codeinstantiation

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


I'm getting the above error when I build the files below, using cl.exe in VSCode. I've been staring at this code for a full day and can't see the error. I've tried a bunch of things, some irrational, to no avail.

When attempting to build it I get the error:

"/debug"
"/out:D:\Apps_for_C++\PokerWorkspace\RNG\RNG.exe"
"RNG.obj"
"RNG.obj : error LNK2001: unresolved external symbol private: static unsigned __int64 RNG::Seed"
"?Seed@RNG@@0_KA)"
"D:\Apps_for_C++\PokerWorkspace\RNG\RNG.exe : fatal error LNK1120: 1 unresolved externals"

Originally I had the main routine in a separate folder "Main" (see Tasks file) and got the same results. I have since appended it to the RND.cpp file. I also had the RNG.h file in a separate folder "Includes" but have since moved it to the RNG folder.

If I comment out the line "RNG Random;" I get a different error:

"/debug"
"/out:D:\Apps_for_C++\PokerWorkspace\RNG\RNG.exe"
"RNG.obj"
"RNG.obj : error LNK2001: unresolved external symbol private: static unsigned __int64 RNG::Seed" "(?Seed@RNG@@0_KA)"
"D:\Apps_for_C++\PokerWorkspace\RNG\RNG.exe : fatal error LNK1120: 1 unresolved externals"

If I delete the main routine it compiles fine but of course fails the linker complaining that there's no main.

It's acting like it isn't seeing the RNG.h file.

It's driving me nuts - can anyone tell me what I'm doing wrong?? I could try some other compilers & linkers, but I'm stubborn and really want to know what's wrong with this approach.

//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"
        }
    ]
}
...



Solution

  • You have Seed static variable which needs to be defined it outside the class. unsigned long long Rng::Seed = some_value;