Article View: comp.std.c++
Article #1644Template class as the friend of a template class
From: rongli
Date: Mon, 07 Aug 1995 18:05
Date: Mon, 07 Aug 1995 18:05
59 lines
1519 bytes
1519 bytes
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 |
---------------------------------------------------------------------
Technical Details
Message-ID:
<405kku$hun@sun.cr.usgs.gov>Path:
arc1-ca.pugleaf.net!mcdgs01.cr.usgs.gov!rongliShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!mcdgs01.cr.usgs.gov!rongli From: rongli@mcdgs01.cr.usgs.gov () Newsgroups: comp.std.c++ Subject: Template class as the friend of a template class Date: 7 Aug 1995 18:05:18 GMT Organization: U.S. Geological Survey, Mid-Continent Sender: rongli@resdgw16.er.usgs.gov () Distribution: world Message-ID: <405kku$hun@sun.cr.usgs.gov> NNTP-Posting-Host: resdgw16.er.usgs.gov