标题:新手求解一个多重继承的访问问题
只看楼主
LBY_CHINA
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2013-7-9
得分:0 
回复 10楼 love云彩
Compiling...
mm.cpp
d:\program files\microsoft visual studio\myprojects\mb\mm.cpp(42) : error C2039: 'base2' : is not a member of 'b22'
        d:\program files\microsoft visual studio\myprojects\mb\mm.cpp(36) : see declaration of 'b22'
d:\program files\microsoft visual studio\myprojects\mb\mm.cpp(42) : error C2385: 'kk::lby' is ambiguous
d:\program files\microsoft visual studio\myprojects\mb\mm.cpp(42) : warning C4385: could be the 'lby' in base 'base' of base 'base1' of base 'b12' of class 'kk'
d:\program files\microsoft visual studio\myprojects\mb\mm.cpp(42) : warning C4385: or the 'lby' in base 'base' of base 'base2' of base 'b12' of class 'kk'
d:\program files\microsoft visual studio\myprojects\mb\mm.cpp(42) : warning C4385: or the 'lby' in base 'base' of base 'base1' of base 'b22' of class 'kk'
d:\program files\microsoft visual studio\myprojects\mb\mm.cpp(42) : warning C4385: or the 'lby' in base 'base' of base 'base2' of base 'b22' of class 'kk'
d:\program files\microsoft visual studio\myprojects\mb\mm.cpp(44) : error C2039: 'base2' : is not a member of 'b22'
        d:\program files\microsoft visual studio\myprojects\mb\mm.cpp(36) : see declaration of 'b22'
d:\program files\microsoft visual studio\myprojects\mb\mm.cpp(44) : error C2385: 'kk::lby' is ambiguous
d:\program files\microsoft visual studio\myprojects\mb\mm.cpp(44) : warning C4385: could be the 'lby' in base 'base' of base 'base1' of base 'b12' of class 'kk'
d:\program files\microsoft visual studio\myprojects\mb\mm.cpp(44) : warning C4385: or the 'lby' in base 'base' of base 'base2' of base 'b12' of class 'kk'
d:\program files\microsoft visual studio\myprojects\mb\mm.cpp(44) : warning C4385: or the 'lby' in base 'base' of base 'base1' of base 'b22' of class 'kk'
d:\program files\microsoft visual studio\myprojects\mb\mm.cpp(44) : warning C4385: or the 'lby' in base 'base' of base 'base2' of base 'b22' of class 'kk'
执行 cl.exe 时出错.

mm.obj - 1 error(s), 0 warning(s)
我的不行,不知为何??
2013-07-09 15:28
love云彩
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:青藏高原
等 级:贵宾
威 望:53
帖 子:3663
专家分:11416
注 册:2012-11-17
得分:0 
回复 11楼 LBY_CHINA
你应该是把版主的代码复制上去进行编译吧?为什么不换一些新的方法来尝试?我现在都是用新版本的C++资料书了,所以我刚才贴上来的那些代码还算是比较新的,我自己写的那些代码还符合你的要求吧?并且我自己写的那些代码我都在VC和VS2012上调试过了,没有错

思考赐予新生,时间在于定义
2013-07-09 15:32
love云彩
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:青藏高原
等 级:贵宾
威 望:53
帖 子:3663
专家分:11416
注 册:2012-11-17
得分:0 
回复 9楼 LBY_CHINA
--------------------Configuration: aaaaa - Win32 Debug--------------------
Compiling...
aaaaa.cpp
c:\windows\system32\aaaaa.cpp(17) : error C2039: 'base2' : is not a member of 'b22'
        c:\windows\system32\aaaaa.cpp(11) : see declaration of 'b22'
c:\windows\system32\aaaaa.cpp(17) : error C2385: 'kk::lby' is ambiguous
c:\windows\system32\aaaaa.cpp(17) : warning C4385: could be the 'lby' in base 'base' of base 'base1' of base 'b12' of class 'kk'
c:\windows\system32\aaaaa.cpp(17) : warning C4385: or the 'lby' in base 'base' of base 'base2' of base 'b12' of class 'kk'
c:\windows\system32\aaaaa.cpp(17) : warning C4385: or the 'lby' in base 'base' of base 'base1' of base 'b22' of class 'kk'
c:\windows\system32\aaaaa.cpp(17) : warning C4385: or the 'lby' in base 'base' of base 'base2' of base 'b22' of class 'kk'
c:\windows\system32\aaaaa.cpp(19) : error C2039: 'base2' : is not a member of 'b22'
        c:\windows\system32\aaaaa.cpp(11) : see declaration of 'b22'
c:\windows\system32\aaaaa.cpp(19) : error C2385: 'kk::lby' is ambiguous
c:\windows\system32\aaaaa.cpp(19) : warning C4385: could be the 'lby' in base 'base' of base 'base1' of base 'b12' of class 'kk'
c:\windows\system32\aaaaa.cpp(19) : warning C4385: or the 'lby' in base 'base' of base 'base2' of base 'b12' of class 'kk'
c:\windows\system32\aaaaa.cpp(19) : warning C4385: or the 'lby' in base 'base' of base 'base1' of base 'b22' of class 'kk'
c:\windows\system32\aaaaa.cpp(19) : warning C4385: or the 'lby' in base 'base' of base 'base2' of base 'b22' of class 'kk'
Error executing cl.exe.

aaaaa.obj - 4 error(s), 8 warning(s)
我复制了版主的代码去调试,有4个错误,你不妨可以看看我的代码,应该还蛮好理解的,都是一步一步来的

思考赐予新生,时间在于定义
2013-07-09 15:37
LBY_CHINA
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2013-7-9
得分:0 
回复 12楼 love云彩
上面就是复制的结果,并且如果删去
// bbb.b22::base2::lby = 1999;
    // 或
    //bbb.b22::base2::base::lby = 1999;
这两句就能编译通过,难道是我的编译器有问题。
2013-07-09 15:37
LBY_CHINA
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2013-7-9
得分:0 
回复 12楼 love云彩
对了,对于二重深度的访问,形如。A.BB1::F(),A.BB2::F()这样的表达式是没有问题的。但是再写下去三重深度
A.BB1::CCC1::F()这样类型的表达式的形式就不行了。
2013-07-09 15:50
love云彩
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:青藏高原
等 级:贵宾
威 望:53
帖 子:3663
专家分:11416
注 册:2012-11-17
得分:0 
我想问一下楼主用的是哪本书?如果在类的多重继承里面,那个说法就不一定了,今晚再举个例子跟你说

思考赐予新生,时间在于定义
2013-07-09 17:46
love云彩
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:青藏高原
等 级:贵宾
威 望:53
帖 子:3663
专家分:11416
注 册:2012-11-17
得分:10 
回复 15楼 LBY_CHINA
程序代码:
#include<iostream>
using namespace std;
class B0            //声明基类B0
{
public:
    B0(int n):nV(n){cout<<"B0的构造函数被调用"<<endl;}
    void fun(){cout<<"member of B0"<<endl;}
    int nV;
};
class B1:virtual public B0   //定义派生类B1,B0是B1的虚基类
{
public:
    B1(int n,int a):B0(n){nV1=a;cout<<"B1的构造函数被调用"<<endl;}
protected:
    int nV1;
};
class B2:virtual public B0  //定义派生类B2,B0是B2的虚基类
{
public:
    B2(int n,int b):B0(n),nV2(b){cout<<"B2的构造函数被调用"<<endl;}
protected:
    int nV2;
};
class D1:public B1,public B2  //定义多重继承的派生类D1
{
protected:
    int nVd;
public:
    D1(int n,int a,int b,int c):B0(n),B1(n,a),B2(n,b),nVd(c){cout<<"D1的构造函数被调用"<<endl;}
    void fun()
    {
        cout<<"member of D1"<<endl;
        cout<<"nV:"<<nV<<endl
            <<"nV1:"<<nV1<<endl
            <<"nV2:"<<nV2<<endl
            <<"nVd:"<<nVd<<endl;
    }
};
void main()
{
    D1 d1(1,2,3,4);
    d1.B0::fun();   //通过派生类对象d1调用基类B0的成员函数
    d1.fun();
    d1.nV=2;        //通过d1调用基类B0的数据成员进行赋值
    cout<<"nV change to:"<<d1.nV<<endl;
}

以上的代码是出自现在最新的C++教材,对于多重继承方面的问题,如果想要调用基类的某个成员函数,可以通过派生类,例如d1.B0::fun(); 就算有多少多重继承,都是差不多这样的,根本不需要像楼主你所说的A.B::C::fun(),既然是继承过来的,若想通过派生类A来调用基类C的fun(),就不需要中间的那个B,直接是A.C::fun()

思考赐予新生,时间在于定义
2013-07-09 18:59



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-416963-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.053707 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved