0

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   
valiano
  • 16,433
  • 7
  • 64
  • 79
Jonitoi
  • 11
  • 3
  • 2
    That ASCII skull tho – MechMK1 Dec 23 '17 at 11:29
  • You should post the full error message – Asesh Dec 23 '17 at 11:31
  • 1
    When posting questions about build errors, always copy (as text) the complete and full output, and paste it (without modifications) into the question body. Please take some time to [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). – Some programmer dude Dec 23 '17 at 11:33
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – user0042 Dec 23 '17 at 11:39
  • ``upToDate`` is a static member variable. You should initialize it at global scope – Asesh Dec 23 '17 at 11:58

1 Answers1

0

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;
Asesh
  • 3,186
  • 2
  • 21
  • 31