So I have been stuck on this problem of sharing a function from one class to another and every solution I have found so far as not solved my problem.
One instance is here(I assure you there are others), [http://software.intel.com/en-us/articles/cdiag436]
Bar.h
#ifndef __Bar_h_
#define __Bar_h_
#include "BaseApplication.h"
#include <Foo.h>
class Foo;
Foo *foo;
class Bar : BaseApplication
{
public:
Bar(void);
~Bar(void);
protected:
virtual void barCreate(void);
};
#endif
Bar.cpp
#include "Bar.h"
#include <Foo.h>
Bar::Bar(void){}
Bar::~Bar(void){}
void Bar::barCreate(void)
{
if(foo->foobar()==true) //error: pointer to incomplete class type is not allowed
{//stuff}
}
Foo.h
#ifndef __foo_h_
#define __foo_h_
class Foo
{
public:
Foo(void);
~Foo(void);
bool foobar(void);
};
#endif
Foo.cpp
#include "Foo.h"
Foo::Foo(void){}
bool Foo::foobar(void){ return true; }
If I could get some pointers or explanation of where i’m going wrong that would be great.
You are misreading the error. It is not complaining about having a pointer to an incomplete type, but about dereferencing it.
At this point the type of
foois an incomplete type, so the compiler cannot check whether it will have afoobarmember function (or member functor).Basically for an incomplete type you can declare and define pointers or references, declare interfaces (functions that take or return the type). But other than that you cannot create objects of the type, or use the pointers/references for anything other than copying the pointer/reference.
Regarding what you are doing wrong, you need to look to your real files in more details. Either you are not including the header that defines
Foo, or you have multipleFootypes (different namespaces? one defines the type, another has the forward declaration) or your include guards are wrong and even if you include the header the guard discards the contents of the header. Note that after inclusion of the header that definesFooyou don’t need to (and should not) provide a forward declaration of the type, as that can easily lead to multiple declarations ofFooin different contexts. If removing the forward declaration fails to compile, figure out why and fix that.