【问题标题】:Link 2001 error with unresolved external symbol使用未解析的外部符号链接 2001 错误
【发布时间】:2017-12-23 11:28:19
【问题描述】:

所以我的小程序中有多个文件,发生的事情是我的一个头文件和 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   

【问题讨论】:

标签: c++ linker-errors


【解决方案1】:

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;

【讨论】:

    猜你喜欢
    • 2014-09-21
    • 1970-01-01
    • 2019-02-26
    • 2012-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    相关资源
    最近更新 更多