home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 20036 < prev    next >
Encoding:
Text File  |  1993-01-28  |  4.2 KB  |  123 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!ukma!gatech!ncar!mimbres.cs.unm.edu!constellation!a.cs.okstate.edu!richarm
  3. From: richarm@a.cs.okstate.edu (MCINTYRE RICHARD D)
  4. Subject: Friends, Template Classes, and Order of Declaration?
  5. Message-ID: <1993Jan28.161016.7308@a.cs.okstate.edu>
  6. Organization: Oklahoma State University, Computer Science, Stillwater
  7. Date: Thu, 28 Jan 93 16:10:16 GMT
  8. Lines: 113
  9.  
  10. Can anyone explain to me some anomolies I get when
  11. I make changes to the code below?  This is a very simlified
  12. example of a similar situation that I'm in.  If I swap the
  13. order of lines 53 and 54, then line 36 gets flagged with an
  14. error message stating:
  15.   'd1<int>' must be previously defined class or struct.
  16.  
  17. Since d1<int> is an abstract base class, I can't have a real
  18. instantiation of that type, even though the pointer to it
  19. should be legal.
  20.  
  21. In my actual situation, I have several classes, and there
  22. does not seem to be a suitable order of declaration that
  23. will resolve the problem.  I have made sure that within the
  24. class definitions I don't have any objects of a type that
  25. has not been declared yet, and I have no pointers to such
  26. objects either.
  27.  
  28. To confound the situation further, the friend declaration on
  29. line 12 affects things somehow.  The friend declaration cannot
  30. be removed without changing the 'private:' declaration on
  31. line 20 to either 'protected:' or 'public:'.
  32.  
  33. In my actual situation, I reluctantly had to grant public
  34. visability to what I would rather have as protected data and
  35. functions in my base classes.  Then I had to remove the friend
  36. declarations so that I could get the compiler to ignore the
  37. order of declarations.  This in addition to some other
  38. compromises I won't go into.
  39.  
  40. If it makes any difference, I'm using BC++ 3.1.  I'd be curious
  41. to know if a similar message appears on any other compilers.
  42. I don't have access to another compiler that supports templates,
  43. or I'd test it myself.
  44.  
  45. //>--------------------- CUT HERE -----------------------<
  46.  
  47. #include <strstream.h>
  48. /* 1: */
  49. /* 2: */
  50. /* 3: */ //*******************************************************
  51. /* 4: */ class b2 {
  52. /* 5: */    protected:
  53. /* 6: */        virtual int getcount( void ) const = 0;
  54. /* 7: */    };
  55. /* 8: */
  56. /* 9: */ //*******************************************************
  57. /*10: */ template <class Type>
  58. /*11: */ class b1 {
  59. /*12: */     friend class d2<Type>;
  60. /*13: */
  61. /*14: */    public:    b1( void ) { count = 0; }
  62. /*15: */      virtual ~b1( void ) {}
  63. /*16: */      virtual void foo( void ) {}
  64. /*17: */
  65. /*18: */    protected: int count;
  66. /*19: */
  67. /*20: */       private: int x;
  68. /*21: */         void inc_x( void ){ ++x; }
  69. /*22: */    };
  70. /*23: */
  71. /*24: */ //*******************************************************
  72. /*25: */ template <class Type>
  73. /*26: */ class d1 : protected b1<Type> {
  74. /*27: */    public:
  75. /*28: */      virtual ~d1( void ) {};
  76. /*29: */      virtual int getnum( void ) const = 0;
  77. /*30: */
  78. /*31: */    protected: int num;
  79. /*32: */    };
  80. /*33: */
  81. /*34: */ //*******************************************************
  82. /*35: */ template <class Type>
  83. /*36: */ class d2 : private d1<Type>, private b2 {
  84. /*37: */
  85. /*38: */    public:
  86. /*39: */      d2( void ) {}
  87. /*40: */     ~d2( void ) {};
  88. /*41: */
  89. /*42: */      int getnum( void ) const { return num; }
  90. /*43: */
  91. /*44: */      // INHERITED VIRTUAL FUNCTION FROM CLASS b2
  92. /*45: */      int getcount( void )
  93. /*46: */        { inc_x();
  94. /*47: */          return count; }
  95. /*48: */      };
  96. /*49: */
  97. /*50: */ //*******************************************************
  98. /*51: */ void main( void )
  99. /*52: */  {
  100. /*53: */  d2<int> *d2x; // If this line gets swapped with #54, then
  101. /*54: */  d1<int> *d1x; // I get an error as described above.
  102. /*55: */  b1<int> b1x;
  103. /*56: */  }
  104.  
  105. //>------------------ CUT HERE ----------------------------<
  106.  
  107. As a side note, does anyone know why the C++ Report costs
  108. $4.95 off the newsrack, but is $69 to subscribe?  A difference
  109. of $24.45 to have it delivered to your mailbox!  Postage and
  110. Handling?
  111.  
  112. Thanx in advance to anyone who can shed some light on either
  113. one of these mysteries.  I read the postings here quite regularly
  114. so followups will be fine.
  115.  
  116. Regards.
  117.  
  118. -- 
  119. Name :            Dallas McIntyre
  120. Occupation :        Student - Still looking for a "real" job.
  121. Email Address :        richarm@a.cs.okstate.edu
  122. Favorite Far Side : Tarzan Introducing Himself to Jane.
  123.