1

can I program in c++ by just using header files?

I keep trying to do it but getting a problem with this file here.. it keeps saying things like:

Severity Code Description Project File Line Suppression State Error LNK2001 unresolved external symbol "private: static int Engine::frameDelay" (?frameDelay@Engine@@0HA) Engine C:\Users\Shadowblitz16\Documents\Visual Studio 2017\Projects\Zelda Classic 3.0\Zelda Classic 3.0\Test.obj 1

Code:

#pragma once
#include "SDL.h"
#include "SDL_image.h"
#include <iostream>

class Engine
{
    private:
        static int frameTime;
        static int frameStart;
        static int frameDelay;
        static bool running;
        static SDL_Window   *window;
        static SDL_Renderer *renderer;

        static void HandleLoop()
        {
            while (running)              //Loop
            {
                //Start frame
                frameStart = SDL_GetTicks();
                HandleUpdate();
                HandleRender();

                //End frame 
                frameTime = SDL_GetTicks() - frameStart;
                if (frameDelay > frameTime) SDL_Delay(frameDelay - frameTime);
            }
        };
        static void HandleUpdate()
        {
            SDL_Event event;
            SDL_PollEvent(&event);
            switch (event.type)
            {
            case SDL_QUIT: running = false; break;
            default: break;
            }
            Update();
        };
        static void HandleRender()
        {
            SDL_RenderClear(renderer);
            Render();
            SDL_RenderPresent(renderer);
        }

    public:
        static void Run(const char* title, int x, int y, int width, int height, bool fullscreen)
        {
            frameTime  = 0;
            frameStart = 0;
            frameDelay = 0;

            window     = nullptr;
            renderer   = nullptr;

            running = false;

            if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
            {
                std::cout << "SDL Initalized!..." << std::endl;
                window = SDL_CreateWindow(title, x, y, width, height, (fullscreen == true ? SDL_WINDOW_FULLSCREEN : 0));
                if (window) std::cout << "Window Created!" << std::endl;

                renderer = SDL_CreateRenderer(window, -1, 0);
                if (renderer) std::cout << "Renderer Created!" << std::endl;

                SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
                running = true;
            }

            frameStart = SDL_GetTicks(); //Get this so we can use it in the create event
            Create();                    //Run create event

            Clean();                     //Run clean event
            SDL_Quit();                  //Quit
        };

        static void Create() {};
        static void Update() {};
        static void Render() {};
        static void Clean()  {};

        static bool Running()              { return running;  };
        static SDL_Window*   GetWindow()   { return window;   };
        static SDL_Renderer* GetRenderer() { return renderer; };
};

I think that its easier to read and debug this way, hopefully this is possiable

Joseph D.
  • 10,071
  • 3
  • 21
  • 54
Shadowblitz16
  • 139
  • 1
  • 10
  • 2
    ***I think that its easier to read and debug this way*** Maybe for some simple programs. However my projects have 10 thousand source files. – drescherjm May 02 '18 at 04:11
  • Your static variables need to be defined. – drescherjm May 02 '18 at 04:14
  • https://stackoverflow.com/questions/18860895/how-to-initialize-static-members-in-the-header?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – drescherjm May 02 '18 at 04:15
  • I don't understand what you mean by define. if I set them to a value at the top then it complains that a member with an in-class initializer must be const. – Shadowblitz16 May 02 '18 at 04:18
  • Both links show this. – drescherjm May 02 '18 at 04:20
  • [what-is-the-difference-between-a-definition-and-a-declaration](http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration) – MFisherKDX May 02 '18 at 04:26
  • I assume, once you've got more than just a few files, you will get an abysmal compiling performance. C++ is - in opposite to Java or C# - not optimized for header-only classes. They do have their right to exist in the shape of templates (which are only possible as header-only), but for standard classes I think header-only-classes are a misuse of the language. – user2328447 May 02 '18 at 04:31
  • You can only check syntax errors with a single header. Under GCC, you can use [-fsyntax-only](https://gcc.gnu.org/onlinedocs/gcc-7.3.0/gcc/Warning-Options.html#Warning-Options), but I don't know if MSVC has such feature. – xskxzr May 02 '18 at 07:27
  • 1
    Since everything's static, there is no point in writing that code inside a class. – molbdnilo May 02 '18 at 07:33
  • I don't know why this is marked duplicate since the dupe they linked is using cpp files but is there a way around this error without using a cpp file? – Shadowblitz16 May 02 '18 at 15:10

0 Answers0