home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 12859 < prev    next >
Encoding:
Text File  |  1992-08-25  |  2.3 KB  |  113 lines

  1. Path: sparky!uunet!ogicse!usenet.coe.montana.edu!rpi!masscomp!macdonal
  2. From: macdonal@westford.ccur.com (Steve MacDonald)
  3. Newsgroups: comp.lang.c++
  4. Subject: Turbo C++ vs. Gnu C++
  5. Message-ID: <1992Aug25.165224.27556@westford.ccur.com>
  6. Date: 25 Aug 92 16:52:24 GMT
  7. Article-I.D.: westford.1992Aug25.165224.27556
  8. Sender: usenet@westford.ccur.com (UNIX news)
  9. Distribution: usa
  10. Organization: Concurrent Computer Corp.  Westford, MA
  11. Lines: 100
  12.  
  13. I have a question regarding the following code. This compiles fine with the 
  14. Gnu C++ compiler, but when I compile this on the latest version of Turbo C++
  15. on a p.c., I get an error. I'll flag the line to the right of where it appears.
  16.  
  17. #include <stream.h>
  18. #include <string.h>
  19. #include <new.h>
  20. #define endl "\n"
  21.  
  22.             //Example3_8
  23. int classcount;        //Accessing an array of class pointers.
  24.  
  25. class Test{
  26. public:
  27.     Test() {
  28.              print(word = "NO INITIALIZER SUPPLIED");
  29.          count = ++classcount;
  30.          }
  31.  
  32.     Test(char *mess ="HELLO") 
  33.         {
  34.          print(word = mess);
  35.          count = ++classcount;
  36.          }
  37.  
  38.     Test(char *mess1, char *mess2){
  39.         print(word = mess2);
  40.         count = ++classcount;
  41.               }
  42.  
  43.     Test(Test &clone){
  44.         print("I'm a clone class");
  45.         count = ++classcount;
  46.         word = clone.word;
  47.         }
  48.  
  49.     ~Test() {
  50.           print ("GoodBye");
  51.           classcount--;
  52.           }
  53.  
  54.     void print(char *mess){
  55.         cout<<mess<<endl;
  56.         }
  57.  
  58.     void print() {
  59.         cout<<word<<endl;
  60.         }
  61.  
  62.     int getcount(void){
  63.         return count;
  64.         }
  65.  
  66. protected:
  67.     char *word;
  68.     int count;
  69. };
  70.  
  71. main(){
  72.  
  73. Test *classptrarray[10];
  74.  
  75. for(int i = 0; i < 10; i++)
  76.     classptrarray[i] = new Test; ***TURBO C++ yields the following error
  77.                     message:
  78.                     Error c:\tc\examples.cpp 58: Ambiguity
  79.                     between 'Test::Test()' and 'Test::
  80.                     Test(char near*)' in function main()
  81.  
  82. int a;
  83. for(int x = 0; x < 10; x++){
  84.     a = classptrarray[x] -> getcount();
  85.  
  86.     //a = (*(classptrarray + x)) -> getcount();
  87.     //Alternate pointer syntax
  88.  
  89.         cout<<a<<endl;
  90. }
  91.  
  92. //delete [10] classptrarray;
  93. //Generates a runtime error: "null pointer assignment"
  94. //new "mem" for 10 classes not deleted.
  95.  
  96. //delete classptrarray;
  97. //Same error as above and "new" mem not deleted
  98.  
  99. //This is the correct way to delete 
  100. for(i = 0; i < 10; i++){
  101.   a = classptrarray[i] -> getcount();
  102.   delete classptrarray[i];
  103.   cout<<a<<endl;
  104.  }
  105. }
  106.  
  107.  
  108. Again, when I compile and run this in the Gnu environment, no problems. What is
  109. wrong with this picture?
  110.  
  111.                     Regards,
  112.                     Steve
  113.