注册 登录
编程论坛 VC++/MFC

C++ 运行错误 问题

milantgh 发布于 2013-01-20 17:39, 685 次点击
#include <iostream.h>

class Base1
{
    int b1;

public:
   
    Base1 (int i)
    {
        b1 = i;

        cout << "Constructor Base1." << endl;
    }

    ~Base1 ()
    {
        cout << "Destructor Base1." << endl;
    }

    void Print ()
    {
        cout << b1 << '\t';
    }
};

class Base2
{
    int b2;

public:
   
    Base2 (int i)
    {
        b2 = i;

        cout << "Constructor Base2." << endl;
    }

    ~Base2 ()
    {
        cout << "Destructor Base2." << endl;
    }

    void Print ()
    {
        cout << b2 << '\t';
    }
};

class Base3 :public Base2
{
    int b3;

public:
   
    Base3 (int i, int j) : Base2 (i)
    {
        b3 = j;

        cout << "Constructor Base3." << endl;
    }

    ~Base3 ()
    {
        cout << "Destructor Base3." << endl;
    }

    void Print ()
    {
        Base2:: Print ();

        cout << b3 << '\t';
    }

    class Base4
    {
        int b4;

    public:
        
        Base4 ()
        {
            b4 = 0;

            cout << "Constructor Base4." << endl;
        }

        ~Base4 ()
        {
            cout << "Destructor Base4." << endl;
        }

        void Print ()
        {
            cout << b4 << '\t';
        }
    };
};

class Member
{
    int m;

public:
   
    Member (int i)
    {
        m = i;

        cout << "Constructor Member." << endl;
    }

    ~Member ()
    {
        cout << "Destructor Member." << endl;
    }

    int GetM ()
    {
        return m;
    }


};

class Derived :public Base3, public Base1, public Base4
{
    int d;

    Member mem;

public:
   
    Derived (int i, int j, int k, int l, int q);

    ~Derived ();

    void Print ();
};

Derived:: Derived (int i, int j, int k, int l, int q) : Base1 (i) , Base3 (j, k), mem (l)
{
    d = q;

    cout << "Constructor Derived." << endl;
}

Derived:: ~Derived ()
{
    cout << "Destructor Derived." << endl;
}

void Derived:: Print ()
{
    Base4:: Print ();

    Base1:: Print ();

    Base3:: Print ();

    cout << mem.GetM () << '\t';

    cout << d << endl;
}

void main (void)
{
    Derived obj (1, 2, 3, 4, 5);

    obj.Print ();
}
7 回复
#2
milantgh2013-01-20 17:40
怎么解决 大神帮忙!!!
#3
liao254283012013-01-20 18:10
错误在哪里啊 贴出来啊
#4
milantgh2013-01-20 18:50
回复 3楼 liao25428301
派生类综合示例.cpp
F:\C++编程\派生类综合示例.cpp(128) : error C2504: 'Base4' : base class undefined
F:\C++编程\派生类综合示例.cpp(156) : error C2352: 'Base3::Base4::Print' : illegal call of non-static member function
        F:\C++编程\派生类综合示例.cpp(94) : see declaration of 'Print'
Error executing cl.exe.

派生类综合示例.obj - 2 error(s), 0 warning(s)
#5
milantgh2013-01-20 18:51
错误提示如上。。。
#6
逆风而前2013-02-24 08:23
class Base4
    {
        int b4;

    public:
        
        Base4 ()
        {
            b4 = 0;

            cout << "Constructor Base4." << endl;
        }

        ~Base4 ()
        {
            cout << "Destructor Base4." << endl;
        }

        void Print ()
        {
            cout << b4 << '\t';
        }
    };
};《--此处多了一个“};”删除即可
#7
逆风而前2013-02-24 08:25
class Base3 :public Base2
{
    int b3;

public:
   
    Base3 (int i, int j) : Base2 (i)
    {
        b3 = j;

        cout << "Constructor Base3." << endl;
    }

    ~Base3 ()
    {
        cout << "Destructor Base3." << endl;
    }

    void Print ()
    {
        Base2:: Print ();

        cout << b3 << '\t';
    }
《--此处少了一个“};”加上即可

#8
wzhdong12013-02-27 16:52
改正好的程序如下,已调试执行过,用windiff工具一对比就知道你的错在哪里了
#include <iostream>
using namespace std;
class Base1
{
    int b1;

public:

    Base1 (int i)
    {
        b1 = i;

        cout << "Constructor Base1." << endl;
    }

    ~Base1 ()
    {
        cout << "Destructor Base1." << endl;
    }

    void Print ()
    {
        cout << b1 << '\t';
    }
};

class Base2
{
    int b2;

public:

    Base2 (int i)
    {
        b2 = i;

        cout << "Constructor Base2." << endl;
    }

    ~Base2 ()
    {
        cout << "Destructor Base2." << endl;
    }

    void Print ()
    {
        cout << b2 << '\t';
    }
};

class Base3 :public Base2
{
    int b3;

public:

    Base3 (int i, int j) : Base2 (i)
    {
        b3 = j;

        cout << "Constructor Base3." << endl;
    }

    ~Base3 ()
    {
        cout << "Destructor Base3." << endl;
    }

    void Print ()
    {
        Base2:: Print ();

        cout << b3 << '\t';
    }
};

class Base4
    {
        int b4;

    public:

        Base4 ()
        {
            b4 = 0;

            cout << "Constructor Base4." << endl;
        }

        ~Base4 ()
        {
            cout << "Destructor Base4." << endl;
        }

        void Print ()
        {
            cout << b4 << '\t';
        }
    };


class Member
{
    int m;

public:

    Member (int i)
    {
        m = i;

        cout << "Constructor Member." << endl;
    }

    ~Member ()
    {
        cout << "Destructor Member." << endl;
    }

    int GetM ()
    {
        return m;
    }


};

class Derived :public Base3, public Base1, public Base4
{
    int d;

    Member mem;

public:

    Derived (int i, int j, int k, int l, int q);

    ~Derived ();

    void Print ();
};

Derived:: Derived (int i, int j, int k, int l, int q) : Base1 (i) , Base3 (j, k), mem (l)
{
    d = q;

    cout << "Constructor Derived." << endl;
}

Derived:: ~Derived ()
{
    cout << "Destructor Derived." << endl;
}

void Derived:: Print ()
{
    Base4:: Print ();

    Base1:: Print ();

    Base3:: Print ();

    cout << mem.GetM () << '\t';

    cout << d << endl;
}

void main ()
{
    Derived obj (1, 2, 3, 4, 5);

    obj.Print ();

   
}
1