Help on incomplete type

I have two classes... for example, class a and class b. for class b, i have to write a overloading input operator and I must have a class a member variable in class b. I wrote the overloading input operator, but I can't seem to insert the member variable type a.

example:

void b::input(istream& in)
{
B all;
A first;
in >> first;
all.setFirst(first);
}

it's giving me an error:
error: aggregate 'A first' has incomplete type and cannot be defined

Please Help!
Last edited on
@yangyy: Can you please paste the rest of the program also? Please click on the "Insert Code" button before pasting the code and paste the code between the generated tags.
Is class|struct A defined?

If not, you can do a quick thing here:

1
2
3
class A;

void b::input...


This is often used with circular references. For example:
1
2
3
4
5
6
7
8
9
10
// class_b.h
class A;

class B
{
  private:
    A data;
  public:
    B (A);
};


1
2
3
4
5
6
7
8
9
10
// class_a.h
class B;

class A
{
  private:
    B data;
  public:
    A (B);
};


That way, no matter what order the headers are listed, there won't be troubles with references to incomplete types.

That is the general idea, though you will need to make it fit with your program. Also, is it meant to be "b::input" or "B::input"? It seems your object type is 'B', but you obviously could have two types, 'B' and 'b'.

I hope this helps!
Topic archived. No new replies allowed.