0

I'm attempting to use a class inside a struct, which seems to be no problem if I declare the class first:

class Foo {
}
stuct Bar {
    Foo example[5];    // array of Foos
}

This seems fragile. It should work in whatever order like functions use prototypes for. I tried prototyping them, but this doesn't work.

class Foo;
struct Bar;

stuct Bar {
    Foo example[5];
}
class Foo {
}

Why doesn't this work? Error code C2079 (on visual studio): 'Bar::Foo' uses undefined class 'Foo'. Error code C2148: total size of array must not exceed 0x7fffffff bytes. Both on the errors occur on Foo example[5];

Also, would it be appropriate to keep my function prototypes above the ADTs (abstract data types) so I can use the functions inside them, or should I write the function inside the ADT?

griffin175
  • 131
  • 6
  • 2
    You need a [mcve]. You have countless syntax errors and typos, making it difficult to understand. Also, the error message would be helpful – Passer By Sep 19 '17 at 02:07
  • 1
    You need the complete definition of a class or struct before you use it in a way that requires the size to be known, such as making an array of them. – Retired Ninja Sep 19 '17 at 02:25
  • @Retired Ninja That's helpful, I didn't realize this was called forward declaration so I didn't find it in my search. – griffin175 Sep 19 '17 at 03:23
  • Knowing the name of what you're looking for is often the hardest part. :) – Retired Ninja Sep 19 '17 at 13:54

0 Answers0