Re: again on forward declaration
On 12/1/2010 9:36 AM, Andrea Crotti wrote:
Ok I thought I got it, but still no luck, why does this below doesn't
work?
Because you haven't followed all my suggestions.
I compile with "g++ A.cpp B.cpp" (does the order matter?)
AFAIK, no, order does not matter. The resulting executable's name will
depend on what file you give it first, but you haven't got that far yet.
but no luck, still
B.cpp: In constructor ?B::B()?:
B.cpp:5: error: invalid use of incomplete type ?struct A?
B.h:7: error: forward declaration of ?struct A?
#ifndef B_H
#define B_H
#include<memory>
// if I don't use a pointer below I'm in trouble
class A;
Don't forward-declare, #include
class B
{
private:
A *member;
Didn't you need 'A member;' here? Whatever...
public:
int x;
B();
};
#endif /* B_H */
#ifndef A_H
#define A_H
#include "B.h"
There is no need to include 'B' here is you never use any members of 'B'
in your header.
class A
{
private:
B *b;
public:
void nothing();
};
#endif /* A_H */
Below starts another file, yes? You need to indicate that!
#include "B.h"
B::B()
{
member = new A();
}
Again new file, no? Tell us that! What are we, mind readers? Why do I
have to guess? Jeez...
#include<iostream>
#include "A.h"
void A::nothing()
{
std::cout<< b->x;
You need to give the compiler the *definition* of class 'B' to use its
members. You forgot to #include "B.h" in this TU.
}
int main(int argc, char *argv[])
If you're not using 'argc' and 'argv', why do you write them? Just use
int main()
{
A a;
a.nothing();
return 0;
}
Try to pay attention.
V
--
I do not respond to top-posted replies, please don't ask