0

I am getting this error:

1>Exception.obj : error LNK2001: unresolved external symbol "public: static struct SDL_Window * Exception::window" (?window@Exception@@2PAUSDL_Window@@A)

each time I try to compile the program. I am clearly missing something but I cannot see what I am doing wrong... How can I solve the error? Here are the files:

Exception.h:

#pragma once

#include <SDL.h>
#include <stdio.h>

class Exception
{
public:
    static SDL_Window* window;
    static enum ErrorMessageType{ CONSOLE, WINDOW, BOTH };

    static void initialize(SDL_Window* window);
    static void showErrorMessage(const char* error, Exception::ErrorMessageType messageType);
};

Exception.cpp

#include "Exception.h"

void Exception::initialize(SDL_Window* window)
{
    Exception::window = window;
}

void Exception::showErrorMessage(const char* error, Exception::ErrorMessageType messageType)
{
    switch (messageType)
    {
        case Exception::CONSOLE:
            printf("\n%s\n", error);
        break;

        case Exception::WINDOW:
            SDL_ShowSimpleMessageBox(0, "Error", error, window);
        break;

        case Exception::BOTH:
            printf("\n%s\n", error);
            SDL_ShowSimpleMessageBox(0, "Error", error, window);
        break;
    }
}
Erik W
  • 2,590
  • 4
  • 20
  • 33

2 Answers2

3
class Exception
{
public:
    static SDL_Window* window;

This declares Exception::window, but does not instantiate it.

Somewhere in one of your translation units, you must also instantiate this class member:

SDL_Window *Exception::window;
Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
2

You need to define the static variable in C++ file as follows

 SDL_Window* Exception::window;
Steephen
  • 14,645
  • 7
  • 40
  • 47