home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / gnu / g / bug / 2353 < prev    next >
Encoding:
Text File  |  1993-01-28  |  3.9 KB  |  182 lines

  1. Path: sparky!uunet!stanford.edu!ames!sun-barr!cs.utexas.edu!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!cis.ohio-state.edu!mermaid.micro.umn.edu!jrauser
  2. From: jrauser@mermaid.micro.umn.edu (John Rauser)
  3. Newsgroups: gnu.g++.bug
  4. Subject: Bug in g++-2.3.3
  5. Date: 26 Jan 1993 22:22:32 -0500
  6. Organization: GNUs Not Usenet
  7. Lines: 169
  8. Sender: daemon@cis.ohio-state.edu
  9. Approved: bug-g++@prep.ai.mit.edu
  10. Distribution: gnu
  11. Message-ID: <199301262209.AA23895@mermaid.micro.umn.edu>
  12.  
  13. Hiya...
  14.  
  15. There seems to be a big squishy bug in g++-2.3.3:
  16.  
  17. Script started on Tue Jan 26 15:21:23 1993
  18.  
  19. 3:21pm ~>g++ -v
  20. Reading specs from /usr/gnu/lib/gcc-lib/sparc/2.3.3/specs
  21. gcc version 2.3.3
  22. 3:21pm ~>exit
  23. exit
  24.  
  25. script done on Tue Jan 26 15:21:43 1993
  26.  
  27.  
  28. I was playing around, trying to learn how to use templates and I
  29. decided to implement a stack.  So, I wrote a stack template that
  30. had a constructor which took as an argument, an integer that was to
  31. be the maximum size of the stack.  When I tried to compile it, I got
  32. the following: (source listings follow at the end of the file)
  33.  
  34. 3:17pm ~>g++ -Wall old-stack.cxx
  35. old-stack.cxx: In function `int  main ()':
  36. old-stack.cxx:13: inconsistent return types for method `stack' in class `stack<int>'
  37. old-stack.cxx:13: inconsistent return types for method `stack' in class `stack<float>'
  38. old-stack.cxx: In method `stack<float>::~stack ()':
  39. old-stack.cxx:19: Internal compiler error.
  40. old-stack.cxx:19: Please report this to `bug-g++@prep.ai.mit.edu'.
  41.  
  42.  
  43. Determined, I tried various tricks to try and make the damn thing
  44. compile... I found that if I changed the constructor so that it didn't
  45. take any arguments, it compiled fine.  Still determined to get it to
  46. work the way _I_ wanted (with arguments to the constructor), I tried to
  47. put the body of the constructor inline in the class declaration, (I
  48. tried to just use the inline directive, but that resulted in the same
  49. error) and it compiled fine.  Now I had what I wanted... after much
  50. hair-pulling.  
  51.  
  52. 3:17pm ~>g++ -Wall stack.cxx
  53. 3:17pm ~>
  54. Use "exit" to leave tcsh.
  55. 3:17pm ~>exit
  56. exit
  57.  
  58.  
  59. Hope this helps you folks out....
  60.  
  61. John Rauser
  62. jrauser@staff.tc.umn.edu
  63. jrauser@mermaid.micro.umn.edu
  64. jmr@ddt.biochem.umn.edu
  65. mf12003@sc.msc.edu
  66.  
  67. ***************************************************************
  68. ************* Listing of stack.cxx       **********************
  69. ************* This is the one that works **********************
  70. ***************************************************************
  71.  
  72. #include<stdio.h>
  73. #include<iostream.h>
  74.  
  75. template <class T> class stack {
  76.  public:
  77.   stack (int maxsize) 
  78.     {
  79.       data = new T[maxsize];  //This code not inline makes g++ puke.
  80.       top = 0;
  81.     }
  82.  ~stack ();
  83.   T        push (T item);
  84.   T        pop ();
  85.  private:
  86.   int top;
  87.   T* data;
  88. };
  89.  
  90. template <class T> stack<T>::~stack () 
  91. {
  92.   delete data;
  93. }
  94.  
  95. template <class T> T stack<T>::push (T item) 
  96. {
  97.    data[top++] = item;
  98.    return item;
  99. }
  100.  
  101. template <class T> T stack<T>::pop () 
  102. {
  103.   return data[--top];
  104. }
  105.  
  106. int main () 
  107. {
  108.   stack<int> s1(20);
  109.   stack<float> s2(20);
  110.  
  111.   s1.push (10);
  112.   printf ("%d\n",s1.pop());
  113.  
  114.   s2.push (10.43);
  115.   printf ("%f\n",s2.pop());
  116.  
  117. }
  118.    
  119.  
  120.  
  121. **************************************************************
  122. ********* Listing of old-stack.cxx ***************************
  123. ********* This one doesn't work    ***************************
  124. **************************************************************
  125. #include<stdio.h>
  126. #include<iostream.h>
  127.  
  128. template <class T> class stack {
  129.  public:
  130.   stack (int maxsize);
  131.  ~stack ();
  132.   T        push (T item);
  133.   T        pop ();
  134.  private:
  135.   int top;
  136.   T* data;
  137. };
  138.  
  139. template <class T> stack<T>::stack (int maxsize)
  140. {
  141.   data = new T[maxsize];
  142.   top = 0;
  143. }
  144.  
  145. template <class T> stack<T>::~stack () 
  146. {
  147.   delete data;
  148. }
  149.  
  150. template <class T> T stack<T>::push (T item) 
  151. {
  152.    data[top++] = item;
  153.    return item;
  154. }
  155.  
  156. template <class T> T stack<T>::pop () 
  157. {
  158.   return data[--top];
  159. }
  160.  
  161. int main () 
  162. {
  163.   stack<int> s1(20);
  164.   stack<float> s2(20);
  165.  
  166.   s1.push (10);
  167.   printf ("%d\n",s1.pop());
  168.  
  169.   s2.push (10.43);
  170.   printf ("%f\n",s2.pop());
  171.  
  172. }
  173.    
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.