1

My code has the same structure as the shown below. I have two container classes defined in a single header file and each one of them has a friend function with parameters of type the other class so I get a compiling error which is something like 'Class2' - undeclared identifier.

Tried a few things but didn't work it out. I think that if add one more template parameter V to both of the templates and replace Class2<T> with it could be a solution but thing get much complicated if I use these containers in my program.I also thought to separate Class1 and Class2 into different headers and then include in Class1 Class2 and vice versa but I actually I doubt that this could work at all.

I really can't figure out how to solve this problem so please your help is much appreciated!

template<class T>
class Class1
{
  ...
  friend void function1(Class1<Class2<T>>&, const Class2<T>&);
  ...
};

template<class V>
class Class2
{
   ...
   friend void function2(Class1<V>);
   ...
};
Omnifarious
  • 54,333
  • 19
  • 131
  • 194
32l3n
  • 95
  • 1
  • 7

1 Answers1

3

Add a forward declaration for Class2 at the beginning of the file:

template<class V> class Class2;
Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186