简体   繁体   中英

Link 2001 error with unresolved external symbol

So i have multiple files in my little program, what is happening is i am getting link2001 errors between one of my header files and cpp files.

struct startup
{
    static std::string latestVersion;
    static std::string currentVersion;
    static std::string latestUpdate;
    static bool upToDate;
    static void checkUpToDate();
    static void consoleStartup();

};

That is my header file and here is my cpp file:

    #include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <string>

void startup::checkUpToDate()
{
    if (startup::currentVersion == startup::latestVersion)
    {
        startup::upToDate = true;
    }
    if (startup::currentVersion != startup::latestVersion)
    {
        startup::upToDate = false;
    }
}
void startup::consoleStartup()
{
    startup::checkUpToDate();
    HANDLE hConsole;
    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    FlushConsoleInputBuffer(hConsole);
    SetConsoleTextAttribute(hConsole, color::red);
    std::cout << R"(
         _,.-------.,_
     ,;~'             '~;,
   ,;                     ;,
  ;                         ;
 ,'                         ',
,;                           ;,
; ;      .           .      ; ;
| ;   ______       ______   ; |
|  `/~"     ~" . "~     "~\'  |
|  ~  ,-~~~^~, | ,~^~~~-,  ~  |
 |   |        }:{        |   |
 |   l       / | \       !   |
 .~  (__,.--" .^. "--.,__)  ~.
 |     ---;' / | \ `;---     |
  \__.       \/^\/       .__/
   V| \                 / |V
    | |T~\___!___!___/~T| |
    | |`IIII_I_I_I_IIII'| |
    |  \,III I I I III,/  |
     \   `~~~~~~~~~~'    /
       \   .       .   /     
         \.    ^    ./
)" << std::endl;

    SetConsoleTextAttribute(hConsole, color::green);
    std::cout << "---------------------The ----------------------" << std::endl;
    SetConsoleTextAttribute(hConsole, color::purple);
    if (startup::upToDate == true)
    {
        std::cout << "      [You are all up to date! Your version: " + startup::currentVersion + " Latest version: " + startup::latestVersion + "]" << std::endl;
    }
    else if (startup::upToDate == false)
    {
        std::cout << "      [You are running on a old update! Your version: " + startup::currentVersion + " Latest version: " + startup::latestVersion + "]" << std::endl;
    }
    SetConsoleTextAttribute(hConsole, color::white);
}

Everything worked fine before i moved it to separate file and had everything in main.cpp. I am not 100% certain what i am doing wrong although i know what i link2001 error is i can't seem to be able to fix it.

My code may also be very bad, im still learning, thanks in advance :)

Also here is the error message:

Severity    Code    Description Project File    Line    Suppression State
Error   LNK2001 unresolved external symbol "public: static bool startup::upToDate" (?upToDate@startup@@2_NA)    FileEncryptionDecryptions   C:\Users\Jonitoi\Desktop\Projects\Visual Studio\cpp\FileEncryptionDecryptions\FileEncryptionDecryptions\startup.obj 1   

upToDate is a static member variable so you should initialize it at global scope:

struct startup
{
    static std::string latestVersion;
    static std::string currentVersion;
    static std::string latestUpdate;
    static bool upToDate;
    static void checkUpToDate();
    static void consoleStartup();
};

bool startup::upToDate = false;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM