size of Object c++ compile problem

This is a discussion on size of Object c++ compile problem within the C++ Programming forums, part of the General Programming Boards category; Could someone please tell me why my main function is not compiling, and how (if you know how to use ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    18

    size of Object c++ compile problem

    Could someone please tell me why my main function is not compiling, and how (if you know how to use g++) would I find the size of objects X and Y? Thanks



    Code:
    int main()
    {
    X *px = new X;
    }
    
    class Y : public X  {
    public:
    void f1() {}
    virtual void f2(){}
    int b;
    };
    
    class X {
    public:
    virtual void f1() = 0;
    virtual ~X(){}
    int a;
    static int sa;
    };

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Your compiler usually tells you why it won't compile. They're called "error messages". In this case, I get:
    Code:
    main.cpp(36) : error C2065: 'X' : undeclared identifier
    main.cpp(36) : error C2065: 'px' : undeclared identifier
    main.cpp(36) : error C2061: syntax error : identifier 'X'
    main.cpp(39) : error C2516: 'X' : is not a legal base class
    You'll have to rearrange your class definitions a bit.
    Code:
    class X {
    public:
    virtual void f1() = 0;
    virtual ~X(){}
    int a;
    static int sa;
    };
    
    class Y : public X  {
    public:
    void f1() {}
    virtual void f2(){}
    int b;
    };
    
    int main()
    {
    X *px = new X;
    }
    Even then it won't work because X is an abstract class. It has a pure virtual function, f1(). You can have a pointer to X, but you cannot make a new X.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    You also need to put in something to take care of that static data member:

    Code:
    int X::sa;
    Finding out the size should be a simple matter of using the sizeof operator:

    Code:
    cout << "Size of X is: " << sizeof(X) << endl;
    cout << "Size of Y is: " << sizeof(Y) << endl;
    Last edited by hk_mp5kpdw; 04-06-2005 at 12:14 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    18
    Hey I hope you are still there , I get it now I solved it. Thanks
    Could you explain how the virtual function tabls are layed out??? I kinda wanna visualize it....but if not thanks problem solved

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    18
    Thanks google man

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  2. Error with a vector
    By Tropicalia in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2006, 07:45 PM
  3. Trouble with DMA Segmentation Faults
    By firestorm717 in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 09:20 PM
  4. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 10:27 AM
  5. DirectMusic engine problem
    By VirtualAce in forum Game Programming
    Replies: 7
    Last Post: 03-17-2005, 05:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21