Thread: no can compile

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    Question no can compile

    this is so wierd...there must be invisible characters on my screen that keeps dev-cpp from compiling it:

    Here's the code that comes with the book that DID compile:

    Code:
    //  Programming with C++, Second Edition, by John R. Hubbard
    //  Copyright McGraw-Hill 2000
    //  Example 12.12, page 285
    //  Memory Leaks
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    class X
    {
    public:
      X() { p = new int[2]; cout << "X().  "; }
      ~X() { delete [] p; cout << "~X().\n"; }
    private:
      int* p;
    };
    
    class Y : public X
    {
    public:
      Y() { q = new int[1023]; cout << "Y():T::q = " << q << ".  "; }
      ~Y() { delete [] q; cout << "~Y().  "; }
    private:
      int* q;
    };
    
    int main()
    {
      for (int i = 0; i < 8; i++)
        {
          X* r = new Y;
          delete r;
        }
      
      system("pause");
      return 0;
        
    }

    ... and here's the code i typed in myself directly from the book with added comments that DID NOT COMPILE:


    Code:
    // Virtual Destructors and Memory Leaks
    // an explicit destructor may be defined to be virtual
    // the following example illustrates the value in defining
    // a virtual destructor
    //
    // since r is declared to be a pointer to X objects, only
    // the X destructor is invoked, deallocating only 8 bytes
    // so on each iteration 4029 bytes are lost!
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    class X
    { public:
        X() { p = new int[2];  cout << "X().  "; }
        ~X() { delete [] p;  cout << "~X().\n"; }
      private:
        int* p;
    };
    
    class Y
    { public:
        Y() { q = new int[1023];  cout << "Y(): Y::q = " << q << ".  "; }
        ~Y() { delete [] q;  cout << "~Y().  "; }
      private:
        int* q;
    };
    
    int main()
    { for (int i=0; i<8; i++)
      { X* r = new Y;
        delete r;
      }
    
      system("pause");
      return 0;
    }
    and here's the error msg the compiler spits out:


    Compiler: Default compiler
    Executing g++.exe...
    g++.exe "C:\C\Code\MemLeak.cpp" -o "C:\C\Code\MemLeak.exe" -Wall -I"C:\DEVCPP\include\c++" -I"C:\DEVCPP\include\c++\mingw32" -I"C:\DEVCPP\include\c++\backward" -I"C:\DEVCPP\include" -L"C:\DEVCPP\lib"
    C:/C/Code/MemLeak.cpp: In function `int main()':
    C:/C/Code/MemLeak.cpp:33: cannot convert `Y*' to `X*' in initialization

    Execution terminated


    I'm not crazy, right?

    Swaine777
    Last edited by Swaine777; 06-24-2003 at 06:05 AM.

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Your version of the program is not deriving Y from X.

    Working...
    Code:
    class Y : public X
    ... yours...
    Code:
    class Y
    ... so you cannot use a base class pointer because there is no base class.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    Lightbulb :}

    whoops! } ~sheepish~

    thanks adrian

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C and C++ compile speed
    By swgh in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-02-2007, 02:37 PM
  2. Compile as you type
    By Rocketmagnet in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 12-07-2006, 01:36 PM
  3. How to compile mfc libs from platform sdk
    By tjcbs in forum Windows Programming
    Replies: 6
    Last Post: 11-19-2006, 08:20 AM
  4. Compile crashes certain windows
    By Loduwijk in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2006, 09:05 PM
  5. How can I compile C or C++ with Visual Studio .NET?
    By Dakkon in forum C Programming
    Replies: 8
    Last Post: 02-11-2003, 02:58 PM