0

For some reason I am getting this error that wasn't showing up before. The error is stating that object of abstract type InitializeState is not allowed. However, i wasn't having this issue before, and I didn't really make any changes to either files. Here is the header file:

#ifndef InitializeState_h
#define InitializeState_h
#include "State.h"
#include "SFML/Graphics.hpp"


class InitializeState : public State{
    public:
        void Load(sf::RenderWindow*);
        //void Load();
        void Handling(SEngine* gameEng);
        void Paint(SEngine* gameEng);
        void Update(SEngine* gameEng);
        void TidyUp();
        void Halt();
        void Continue();
        static InitializeState* Initialize(){
            return &gameStart;
        }


    protected:
        InitializeState() {}

    private:
        static InitializeState gameStart;
};


#endif

Here is the c++ file where the error is occurring:

#include "InitializeState.h"
#include "State.h"
#include "SEngine.h"

#include <SFML/Graphics.hpp>



InitializeState InitializeState::gameStart;

void InitializeState::Load(sf::RenderWindow* window){
    rwindow = window;
    rwindow->create(sf::VideoMode(200,200), "Working");
};

void InitializeState::Handling(SEngine* gameEng){

};

void InitializeState::Paint(SEngine* gameEng){


};

void InitializeState::Update(SEngine* gameEng){

};

void InitializeState::Continue(){
};

void InitializeState::Halt(){
};

The fifth line of code, InitializeState InitializeState::gameStart;, is where the error is occurring.

Here is the error window:

1>------ Build started: Project: 2D Game, Configuration: Debug Win32 ------
1>  SEngine.cpp
1>c:\users\brandon\documents\visual studio 2010\projects\2d game\2d game\sengine.cpp(26): error C2660: 'State::Handling' : function does not take 1 arguments
1>  Main.cpp
1>  InitializeState.cpp
1>c:\users\brandon\documents\visual studio 2010\projects\2d game\2d game\initializestate.cpp(9): error C2259: 'InitializeState' : cannot instantiate abstract class
1>          due to following members:
1>          'void State::Handling(void)' : is abstract
1>          c:\users\brandon\documents\visual studio 2010\projects\2d game\2d game\state.h(14) : see declaration of 'State::Handling'
1>          'void State::Paint(void)' : is abstract
1>          c:\users\brandon\documents\visual studio 2010\projects\2d game\2d game\state.h(15) : see declaration of 'State::Paint'
1>          'void State::Update(void)' : is abstract
1>          c:\users\brandon\documents\visual studio 2010\projects\2d game\2d game\state.h(16) : see declaration of 'State::Update'
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
grim_v3.0
  • 387
  • 1
  • 4
  • 11
  • "object of abstract type InitializeState is not allowed" just like i said at the top of the post. – grim_v3.0 Jan 29 '14 at 07:42
  • What compiler is this? The output is typically more verbose like stating why the object is abstract (as in "you forgot to implement method X") – Samuel Jan 29 '14 at 07:43
  • Visual Studio Express 2010, and it says that methods in its inherited class were abstract, but they were always like that even before the error started happening. – grim_v3.0 Jan 29 '14 at 07:46
  • Post the `Output`-Window message word by word, not the `Error List`-Window message – Samuel Jan 29 '14 at 07:47
  • the override of the functions Handling,Update, Paint have different signature in InitializeState. In state they don't take any arguments. – sajas Jan 29 '14 at 07:51
  • Updated my post. You are lacking a virtual dtor here too. I would really suggest learning C++ first. And yes C++ is not easy. – Samuel Jan 29 '14 at 08:03

2 Answers2

0

If you implement

void State::Handling(void) override;
void State::Paint(void) override;
void State::Update(void) override;

From the class State you will be fine.

And you need to use the virtual keyword when overriding as you are performing method hiding otherwise.

The class State requires you to implement the methods above regardless of any similar methods you need with different signatures like your SEngine parameter.

This is basic OOP knowledge you are lacking here. I suggest reading a book like Thinking in C++ 1 and 2 to gain some understanding prior to mess with an engine.

Edit: Another problem with your code: Your destructor must be at least virtual as you inheriting and not using the MS proprietary keyword sealed. You may or probably will get into some memory leaks here.

Some reading: When to use virtual destructors?

Edit: Added new c++11 override sugar, removed virtual. Learned something new :)

Community
  • 1
  • 1
Samuel
  • 6,126
  • 35
  • 70
  • 1
    the `virtual` is unnecessary, and should be replaced by `override` if you use a recent compiler. – Klaim Jan 29 '14 at 07:54
  • Actually it didn't work but it did help me identify the actual problem: apparently the methods in my State.h were modified by accident so that they were different from the classes that implemented them. The methods in State.h had no arguments, so I just had to add them back in. – grim_v3.0 Jan 29 '14 at 07:54
  • You don't need to add `virtual` when overriding. You need to add it to the member declaration in the base class. Adding it in the derived types is optional, but using `override` will let the compiler tell you if you are failing to override for whatever reason (getting the signature wrong, for example). – juanchopanza Jan 29 '14 at 07:55
  • @juanchopanza source? Override is a Microsoft specific keyword afaik, adding it it to derived types is *not* optional as this would cause a method hiding without providing an entry in the vtable and being properly resolved. We are talking about abstract members here. – Samuel Jan 29 '14 at 07:56
  • 1
    @Samuel No, `override` is a C++ special identifier. Its use is at compile time. But the main point is that you **do not** need to use the `virtual` keyword when overriding. – juanchopanza Jan 29 '14 at 08:16
  • @juanchopanza, cool, something I must have overlooked in the c++11 standard. Thanks for the hint. I'll update my post but I have not located a source saying I can omit the virtual here. – Samuel Jan 29 '14 at 08:19
0

The error is stating that object of abstract type InitializeState is not allowed.

This typically means that some of the virtual pure function of the type's parent are not implemented in the children. Basically, you miss some functions definitions in InitializeState.

1>c:\users\brandon\documents\visual studio 2010\projects\2d game\2d game\initializestate.cpp(9): error C2259: 'InitializeState' : cannot instantiate abstract class
1>          due to following members:
1>          'void State::Handling(void)' : is abstract
1>          c:\users\brandon\documents\visual studio 2010\projects\2d game\2d game\state.h(14) : see declaration of 'State::Handling'
1>          'void State::Paint(void)' : is abstract
1>          c:\users\brandon\documents\visual studio 2010\projects\2d game\2d game\state.h(15) : see declaration of 'State::Paint'
1>          'void State::Update(void)' : is abstract
1>          c:\users\brandon\documents\visual studio 2010\projects\2d game\2d game\state.h(16) : see declaration of 'State::Update'

Here you can see the list of member functions from the State class which are missing in the InitializeState class. This is because these functions are pure virtual: they are "required" to be implemented by at least one of the child classes.

Just implement them in InitializeState and that will fix your problem.

Klaim
  • 67,274
  • 36
  • 133
  • 188