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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!timbuk.cray.com!hemlock.cray.com!augs
  3. From: augs@cypress45.cray.com (Paul Algren)
  4. Subject: Why won't this C++ compile?
  5. Message-ID: <1993Jan25.105518.8645@hemlock.cray.com>
  6. Originator: augs@cypress45
  7. Lines: 95
  8. Sender: augs@cypress45 (Paul Algren)
  9. Nntp-Posting-Host: cypress45
  10. Organization: Cray Research, Inc.
  11. Date: 25 Jan 93 10:55:18 CST
  12.  
  13.  
  14. Any comments would be interesting on the following code sequence.
  15.  
  16. Given the following files:
  17.  
  18. // ############# class1.h ##################
  19. class class2;
  20.  
  21. class class1 {
  22.     class2* other;
  23.  
  24.   public:
  25.     class2* class2() {
  26.     return (other);
  27.     }
  28.     void SetClass2(class2*);
  29. };
  30.  
  31. // ############# class2.h ##################
  32. class class1;
  33.  
  34. class class2 {
  35.     class1* other;
  36.  
  37.   public:
  38.     class1* class1() {
  39.     return (other);
  40.     }
  41.     void SetClass1(class1*);
  42. };
  43.  
  44. // ############# class1.c ##################
  45. #include <class1.h>
  46. #include <class2.h>
  47.  
  48. void
  49. class1::SetClass2(class class2* c)
  50. {
  51.     other = c;
  52. }
  53.  
  54. // ############# class2.c ##################
  55. #include <class2.h>
  56. #include <class1.h>
  57.  
  58. void
  59. class2::SetClass1(class1* c)
  60. {
  61.     other = c;
  62. }
  63.  
  64. I compile class1.c (CC -c -I. class1.c) and get no errors, but I compile
  65. class2.c and I get the following errors.
  66.  
  67. (2.1)
  68. CC -c -I. class2.c
  69. "class2.c", line 5: warning: old style definition of SetClass1() (anachronism)
  70. "class2.c", line 8: error:  name expected in argument list
  71. "class2.c", line 5: error:  class2::SetClass1() type mismatch:  void class2::(class1 *) and  void class2::()
  72. Compilation failed
  73.  
  74. Does someone have an explanation for this?
  75. I can't seem to imagine why the compiler searches the symbols finding
  76. class2::class1() first or at all instead of searching for known classes (types).
  77.  
  78. The fix in class1.c is to add the class specifier before the classname
  79. explicitly telling the compiler to expect a class name instead of a 
  80. symbol, but why?
  81.  
  82. When using 3.0 I have the same problem, but there is now another error on the
  83. declaration in the include files.  Now neither class1.c nor class2.c will
  84. compile.
  85.  
  86. (3.0)
  87. CC-3.0.1 -c -I. class2.c
  88. "./class2.h", line 13: error:  initializer for member SetClass1
  89. "./class2.h", line 13: error: bad base type: void SetClass1
  90. "./class1.h", line 13: error:  initializer for member SetClass2
  91. "./class1.h", line 13: error: bad base type: void SetClass2
  92. "class2.c", line 5: warning: old style definition of SetClass1() (anachronism)
  93. "class2.c", line 6: error:  name expected in argument list
  94. "class2.c", line 5: error:  class2::SetClass1() is not a member of class2
  95. Compilation failed
  96.  
  97. C-3.0.1 -c -I. class1.c
  98. "./class1.h", line 13: error:  initializer for member SetClass2
  99. "./class1.h", line 13: error: bad base type: void SetClass2
  100. "./class2.h", line 13: error:  initializer for member SetClass1
  101. "./class2.h", line 13: error: bad base type: void SetClass1
  102. "class1.c", line 5: error:  class1::SetClass2() is not a member of class1
  103. Compilation failed
  104.  
  105. ??????,
  106.  
  107. Paul
  108.