注册 登录
编程论坛 C++教室

高手请看,好像比较的难以解决

shediao 发布于 2008-11-13 09:05, 616 次点击
// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream.h>

class A
{
public:
    //A(){ cout<<"调用无参数构造函数"<<endl;}
    A(int x=0):a(x){ cout<<"调用了A的含参数构造函数"<<endl;}
    void show(){ cout<<"调用A类中的show() a="<<a<<endl; }
private:
    int a;
};


class B:virtual public A
{
public:
    B(int x=0,int y=0):A(x),b(y){}
    void show(){
        cout<<"------------------------------------"<<endl;
        A::show();
        cout<<"调用B类中的show() b="<<b<<endl;
    }
private:
    int b;
};


class C:virtual public A
{
public:
    C(int x=0,int y=0):A(x),c(y){}
    void show(int a){cout<<"CCCC"<<endl;}
private:
    int c;
};


class D:public B,public C
{
public:
    D(int x=0,int y=0,int m=0,int n=0):B(x,y),C(x,m),A(x),d(n){}
    //void show(){}
private:
    int d;
};

int main(int argc, char* argv[])
{
    D d(1,2,3,4);
    d.show();
    return 0;
}

我这个程序是 B C 继承A  D继承B C A类中有个show()函数,B中对其重写 ,C中对其重载 怎么D中调用show()的时候会发生错误呢, c++编程怎么久了 这个问题 还没有解决,请高手帮忙 !! 谢谢大家
5 回复
#2
pascale2008-11-13 09:41
console.cpp:48: 错误: 对成员‘show’的请求有歧义
console.cpp:30: 错误: candidates are: void C::show(int)
console.cpp:16: 错误:                 void B::show()
#3
newyj2008-11-13 11:13
程序代码:
int main(int argc, char* argv[])
{
    D d(1,2,3,4);
    d.B::show();          //d.show() => d.B::show()
    return 0;
}
#4
pascale2008-11-13 11:19
[bo][un]newyj[/un] 在 2008-11-13 11:13 的发言:[/bo]

int main(int argc, char* argv[])
{
    D d(1,2,3,4);
    d.B::show();          //d.show() => d.B::show()
    return 0;
}
 

如果是要调用C中的show函数该怎么写呢?
要传递的参数是A中的private..
#5
shediao2008-11-13 12:10
回复 3# 的帖子
老兄我就是想问一下 为什么 在 d中调用 show()是会发生歧义呢?
1