链接2001错误与未解析的外部符号

时间:2017-12-23 11:28:19

标签: c++ linker-errors

所以我的小程序中有多个文件,发生的事情是我的一个头文件和cpp文件之间出现link2001错误。

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

};

这是我的头文件,这是我的cpp文件:

    #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);
}

在将其移动到单独的文件并将所有内容都放在main.cpp中之前,一切正常。我不是100%肯定我做错了虽然我知道我的link2001错误是我似乎无法解决它。

我的代码也可能非常糟糕,我还在学习,提前谢谢:)

此处还出现错误消息:

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   

1 个答案:

答案 0 :(得分:0)

upToDate是一个静态成员变量,因此您应该在全局范围内初始化它:

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;
相关问题