6

Could anyone, please, explain what can cause this error?

Error: Invalid base class

I've got two classes where one of them is derived from second:

#if !defined(_CGROUND_H)
#define _CGROUND_H

#include "stdafx.h"
#include "CGameObject.h"


class CGround : public CGameObject // CGameObject is said to be "invalid base class"
{
private:
    bool m_bBlocked;
    bool m_bFluid;
    bool m_bWalkable;

public:
    bool draw();

    CGround();
    CGround(int id, std::string name, std::string description, std::string graphics[], bool bBlocked, bool bFluid, bool bWalkable);
    ~CGround(void);
};

#endif  //_CGROUND_H

And CGameObject looks like this:

#if !defined(_CGAMEOBJECT_H)
#define _CGAMEOBJECT_H

#include "stdafx.h"

class CGameObject
{
protected:
    int m_id;
    std::string m_name;
    std::string m_description;
    std::string m_graphics[];

public:
    virtual bool draw();

    CGameObject() {}
    CGameObject(int id, std::string name, std::string description, std::string graphics) {}

    virtual ~CGameObject(void);
};

#endif  //_CGAMEOBJECT_H

I tried cleaning my project but in vain.

dziwna
  • 1,212
  • 2
  • 14
  • 25
  • 2
    A guess but `std::string m_graphics[];` is not standard C++. If it means what I think it means then it would make an invalid base class. I suggest replacing with `std::vector m_graphics;`. – john Nov 09 '12 at 12:59
  • @john m_graphics[] is perfectly valid standard C++. – Nikos C. Nov 09 '12 at 13:01
  • @john It helped, thank you. Type it as anwser please and I will mark it as best one. :) – dziwna Nov 09 '12 at 13:04
  • 1
    std::string in not a POD, so his array need a fixed size – Luke B. Nov 09 '12 at 13:05
  • @LukeB. Oops, I was looking at the argument of `CGround::CGround` instead. I didn't see the member. That's indeed not standard C++. – Nikos C. Nov 09 '12 at 13:06
  • His array needs a fixed size, period. No question of POD or not. He should have gotten an error from `GameObject.h`, unless he's using a broken compiler (or hasn't passed the options needed for the compiler to be a C++ compiler). – James Kanze Nov 09 '12 at 13:07

1 Answers1

8

It is not valid to define an array (std::string m_graphics[]) without specifying its size as member of a class. C++ needs to know the size of a class instance in advance, and this is why you cannot inherit from it as C++ won't know at runtime where in the memory the members of the inheriting class will be available.
You can either fix the size of the array in the class definition or use a pointer and allocate it on the heap or use a vector<string> instead of the array.

Benoit Thiery
  • 6,325
  • 4
  • 22
  • 28