Problems w.r.t Virtual Base Class (inheritance)

This is a discussion on Problems w.r.t Virtual Base Class (inheritance) within the C++ Programming forums, part of the General Programming Boards category; This is a problem I have encountered when inheriting a virtual base class without having default arguments. X (base class) ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    1

    Question Problems w.r.t Virtual Base Class (inheritance)

    This is a problem I have encountered when inheriting a virtual base class without having default arguments.

    X (base class)

    Y( virtual base class, derived from X)

    A(derived class, derived from Y)


    I have made provision in class Y to pass values to the constructor of X but when I try to create an object of class A, it get the following error message

    Error Message:
    ---------------------
    Compiling...
    Cpp2.cpp
    D:\My Projects\SampleProj\Cpp2.cpp(31) : error C2512: 'X::X' : no appropriate default constructor available
    Error executing cl.exe.
    --------------------



    Code:
    ----------------------------
    #include <iostream>
    using namespace std;

    class X
    {
    int i;

    public:
    X(int p){ i = p; }
    void show() { cout << "show:the value of i is " << i << endl; }
    };


    class Y : virtual public X
    {

    public:
    Y(int y) : X(y)
    {
    cout << "In y" << endl;
    }
    };

    class A: public Y
    {

    public:

    A(): Y(10)
    {
    cout << "In A" << endl;
    }
    };


    int main()
    {
    A a1;
    a1.show();
    return 0;
    }

    ------------------------------

    Is there any specific reason for this behaviour. If you can answer this question, would be of great help.

    Regards
    Pankajdynamic

  2. #2
    Unregistered
    Guest
    Try adding a constructor to X:
    X::X(void)
    { }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why Virtual base class increase the size of class by 4 bytes?
    By vaibhavs17 in forum C++ Programming
    Replies: 14
    Last Post: 07-09-2009, 07:55 AM
  2. virtual destructors
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-11-2009, 12:14 PM
  3. Inheritance, overriding, problems
    By CodeMonkey in forum C++ Programming
    Replies: 8
    Last Post: 01-04-2007, 01:26 AM
  4. Replies: 1
    Last Post: 11-27-2001, 01:07 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 02:26 PM

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