Invalid use of incomplete type
Mar 6, 2018 at 8:21pm UTC
Hi
I have two calsses: MyObject and MyLogic
MyObject inherits from MyLogic
but MyLogic needs to have a reference to MyObject.
So i forward declared MyObject in MyLogic but its giving me an
Invalid use of incomplete type
error
Code:
MyObject.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#pragma once
#include <string>
#include "MyLogic.h"
typedef std::string ObjectID;
class MyObject : public MyLogic
{
private :
const ObjectID objectID;
public :
MyObject(ObjectID id);
~MyObject();
const ObjectID GetID();
};
MyObject.cc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include "MyObject.h"
MyObject::MyObject(ObjectID id)
:MyLogic(this )
,objectID(id)
{
}
MyObject::~MyObject()
{
}
const ObjectID MyObject::GetID()
{
return this ->objectID;
}
MyLogic.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#pragma once
#include <vector>
#include <memory>
class MyObject;
class MyLogic
{
private :
MyObject* object;
public :
MyLogic(MyObject* obj);
~MyLogic();
void Update();
};
MyLogic.cc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include "MyLogic.h"
MyLogic::MyLogic(MyObject* obj)
:object(obj)
{
}
MyLogic::~MyLogic()
{
}
void MyLogic::Update()
{
//this->object->GetID(); this results in 'invalid use of incomplete type' error
}
I tried looking it up, and it seems to happen when you circular include,
but i'm not circular including ?
or am i?
Last edited on Mar 6, 2018 at 8:26pm UTC
Mar 6, 2018 at 8:34pm UTC
Include "MyObject.h" in MyLogic.cc.
However, whenever design looks complicated you should ask yourself whether it is an appropriate design. Perhaps some different design could achieve all your goals with less mind-boggling?
Mar 6, 2018 at 8:40pm UTC
oh, of course.
Yeah i think i will try to rethink my design again
Thanks so much :)
Last edited on Mar 6, 2018 at 8:41pm UTC
Topic archived. No new replies allowed.