Topic: Template class as the friend of a template class


Author: rongli@mcdgs01.cr.usgs.gov ()
Date: 1995/08/07
Raw View
 The following codes compiled and runs well on Borland C++ but failed
while compiling using gcc-2.6.3:


// test.cc
#include <iostream.h>

template <class X>
class B
{
  friend class A;
  X data;
public:
  B() : data(1024) {}
  X getData() { return data; }   // I know this will work
};


template <class T, class Y>
class A
{
  T t;
  B<Y> b;
public:
  void display() { cout << "the data is " << b.data << endl;}
};

void main(void)
{
  A<double, int> a;
  a.display();
  return 0;
}



It gave the error as:

test.cc: In method `void A<double,int>::display()':
test.cc:21: member `data' is a private member of class `B<int>'
gmake: *** [test.o] Error 1

 I know if I used getData(), it would work, but that's not the
way I want. Can some one point out that why  friend class A can not access
data and how to work around other than using getData() ?

 Thanks.




--


----------------------------------------------------------------------
 Rong Li   |
 rongli@resdgw16.er.usgs.gov |
---------------------------------------------------------------------