home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / cplus / 17833 < prev    next >
Encoding:
Text File  |  1992-12-11  |  1.0 KB  |  72 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!stanford.edu!EE.Stanford.EDU!bbc!danyang
  3. From: danyang@bbc.Stanford.EDU (Dan Yang)
  4. Subject: question regarding to template
  5. Message-ID: <1992Dec11.225733.119@EE.Stanford.EDU>
  6. Keywords: friend, private member, template
  7. Sender: danyang@bbc (Dan Yang)
  8. Organization: Stanford University
  9. Distribution: su
  10. Date: Fri, 11 Dec 92 22:57:33 GMT
  11. Lines: 59
  12.  
  13.  
  14. -- 
  15. Dan Yang
  16.  
  17.  
  18. I would like to access private member of a template class through friend
  19. declaration. But ATT compiler gave an compiler error. Here is the 
  20. source code. Thank you for your help!!!
  21.  
  22.  
  23. #include <stdio.h>
  24.  
  25. class B;
  26.  
  27. template <class c>
  28. class A
  29. {
  30.   friend B;
  31.  private:
  32.   int i;
  33.  public:
  34.   A(c b) { i = b; a = b; };
  35.   c a;
  36. };
  37.  
  38.  
  39. class B
  40. {
  41.  public:
  42.   int f(A<int> a);
  43. };
  44.  
  45.  
  46. int B::f(A<int> a)
  47. {
  48.    printf("%d\n", a.i);
  49.    return 0;
  50. }
  51.  
  52. main()
  53. {
  54.    A<int> a(10);
  55.    B b;
  56.    b.f(a);
  57.    return 0;
  58.  }
  59.  
  60.  
  61.  
  62. The error message is :
  63.  
  64.  
  65. CC  test.c:
  66. "test.c", line 26: error:  B::f() cannot access  A <int >::i: private  member
  67. "test.c", line 33: warning:  b used but not set
  68. 1 error
  69.  
  70.  
  71.  
  72.