0

I'm having 2 .h files ( with .cpp files ) that has code like that:

Foo:

#ifndef INCLUDE_FOO_H_
#define INCLUDE_FOO_H_

class Foo
{
    public:
        virtual ~Foo();
        virtual void workWithBar(Bar* bar);
};

#endif /* INCLUDE_FOO_H_ */

Bar:

#ifndef INCLUDE_BAR_H_
#define INCLUDE_BAR_H_

class Bar
{
    public:
        virtual ~Bar();
        virtual void letsWorkWithFoo(Foo* itsFoo);
};

class Bar2 : public Bar
{
    public:
        virtual void letsWorkWithFoo(Foo* itsFoo);
};
#endif /* INCLUDE_BAR_H_ */

cpp files are just simple files , with "empty" impl. of that code. I'm using latest Eclipse , and latest MinGW that i could find. Both of cpp files include and . If i put all that classes together , it would compile and work fine. And i'm using Foo.h in main.cpp too , and its working perfect too.

I did try adding include into Foo & Bar , i did try using diffrent std(-std=c++11). I'm not using "using namespace" anywhere in .h files.

p.s. if you didnt undestend it from title -

C:\PathWasHere\include/Bar.h:35:21: error: 'Foo' has not been declared

   virtual void workWithFoo(Foo *Foo);

                     ^
C:\PathWasHere\include/Bar.h:86:21: error: 'Foo' has not been declared

   virtual void workWithFoo(Foo *Foo);
                     ^

edit: forgot to say , i'm working with Bar in Foo too.

fhntv24
  • 384
  • 3
  • 11
  • 1
    you missed `;` after `}` of every class – taocp Oct 01 '14 at 18:32
  • ...include `foo.h` in `bar.h` or forward declare `class Foo;`? You need to declare before any use. – crashmstr Oct 01 '14 at 18:32
  • @taocp sorry , i was writing it in stackoverflow – fhntv24 Oct 01 '14 at 18:33
  • @crashmstr read my problem fully - i sad that i did try including foo.h in bar.h :| – fhntv24 Oct 01 '14 at 18:34
  • Then you need to add that to your code, because everyone is going to tell you that's the problem. – aruisdante Oct 01 '14 at 18:37
  • @fhntv24 I saw that is what you said, but you don't *show* that in your code here. Thus I can only assume you did it wrong or did not actually do it (assuming it was not the issue that taocp points out). – crashmstr Oct 01 '14 at 18:37
  • @aruisdante alredy modified message – fhntv24 Oct 01 '14 at 18:39
  • @crashmstr 1. its included in .cpp file 2. As i sad , i did try #include – fhntv24 Oct 01 '14 at 18:40
  • @fhntv24 OH WOW. You've drastically changed your question now that `Foo` uses `Bar` and vice versa (this was not there before!). Just forward declare the class so you can declare functions using a pointer to the class and include the header in the cpp files. Also, this now is a duplicate of at least one question here already. – crashmstr Oct 01 '14 at 18:43
  • 2
    Research "forward declarations". – Thomas Matthews Oct 01 '14 at 18:45
  • @crashmstr OH WOW , THERE IS NO EVEN WORD 'DECLARED'. How i could find it , if its even called diffrently? – fhntv24 Oct 01 '14 at 18:50
  • @ThomasMatthews ty , i know what it is. I didnt know what C++ need it for that cind of stuff ( was coding in Java , C# , Pascal. None of them need Forward for classes. Pascal need it for funcs , but not for classes. In pascal , it called public and private include - you could just include it privatly , and it would work perfect. ) – fhntv24 Oct 01 '14 at 19:02

0 Answers0