0

I want to implement generic graph classes and I am still having problems which narrowed down to the following code:

template <class T> class B
{
    template <T> friend class A;
};


template <class T> class A
{
private:
    std::vector<B<T> *> myBs;
};


class C { };

This compiles pretty well unless I do something like this:

B<C> myB;

... which causes the following errors:

B.h: In instantiation of ‘class B<C>’:
In file included from A.h:12:0,
             from main.cpp:16:
main.cpp:30:10:   required from here
B.h:15:1: error: ‘class C’ is not a valid type for a template non-type parameter
{
^
B.h:11:11: error: template parameter ‘class T’
template <class T> class A;
          ^
B.h:16:31: error: redeclared here as ‘<declaration error>’
template <T> friend class A;

Is my thinking absolutely wrong, am I missing something or is such a construct not possible, messy, odd or a very (...) very awful thing to do?

Sir Tobi
  • 142
  • 1
  • 8

2 Answers2

3

The problem is your friend declaration. I suggest you declare the A class first, and then use a simpler friend declaration. Like

template<class T> class A;

template<class T>
class B
{
    friend A<T>;
    ...
};
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

The question is whether you want to make just A<T> a friend of B<T>. Then use Joachim's solution!

If you want to make any A a friend, then you need to do this:

template <class T> class B
{
    template<class> friend class A;
};
eigenleif
  • 95
  • 6