Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;继承及;虚函数问题_C++ - Fatal编程技术网

C++ C++;继承及;虚函数问题

C++ C++;继承及;虚函数问题,c++,C++,我想显示结果是2。(现在结果是1。) 我该怎么办?(我想调用B::test(),但实际上代码无法访问main.c中的B.h、B.c) 我还想知道a.h中从“public:virtual int test(){return 1;}”到“protected:virtual int test(){return 1;}”的错误 继承关系是 超A类B类 超A类C类 但是我可以访问main.c中的类 我想要结果2。(“a.test()”无法调用“b.test()”) //main.c #包括 #包括“a.h

我想显示结果是2。(现在结果是1。)

我该怎么办?(我想调用B::test(),但实际上代码无法访问main.c中的B.h、B.c)

我还想知道a.h中从“public:virtual int test(){return 1;}”到“protected:virtual int test(){return 1;}”的错误

继承关系是 超A类B类 超A类C类

但是我可以访问main.c中的类 我想要结果2。(“a.test()”无法调用“b.test()”)

//main.c
#包括
#包括“a.h”
使用名称空间std;
内部主(空)
{
A*A=新的A();
cout test()考虑您的代码:

// main.c
#include <iostream>
#include "a.h"

using namespace std;

int main(void)
{
    A *a = new A();
    cout << a->test() << "\n";
    return 0;
}
将在main中失败,因为它在类之外,并且只能访问
public
成员


还值得指出的是,您的
朋友
声明在所示代码中是完全不必要的。

创建
B
类型的对象,您仍然可以调用
test
方法,因为
A::test
是公共的。此代码将打印2:

int main(无效)
{
A*A=新的B();
//             ^

cout test()我建议您更改标题和标签。这不是多重继承。多重继承是指类上有两个直接父级的地方:
类C:public A,public B…
code无法访问main中的B.h,B.C。为什么?我只发布了实际代码问题的摘要。他的意思是=新的B()将不编译。此限制来自何处?您可以使用极其复杂的工厂模式来实现这一点,但这似乎不太可能是您正在尝试执行的操作。我将执行此操作。谢谢您的建议。main.c无法访问B类和c类(B.h,B.c,c.h,c.c)@AndrewChang添加了一个示例,说明如何处理它。感谢您的建议。但不起作用…/tmp/ccrGMeiu.o:In function
A::A()':main.c:(.text.\u ZN1AC2Ev[\u ZN1AC5Ev]+0xb):未定义对
vtable的引用,用于“collect2:error:ld返回1退出状态
// b.h
#ifndef _B_
#define _B_
#include "a.h"

class B : public A {
    public:
        B() {};
        ~B() {};
    private:
        int test() override;
        friend class A;
};

#endif
// b.c
#include "b.h"

int B::test()
{
    return 2;
}
// c.h
#ifndef _C_
#define _C_
#include "a.h"

class C : public A {
    public:
        C() {};
        ~C() {};
    private:
        int test() override;
        friend class A;
};

#endif
// c.c
#include "c.h"

int C::test()
{
    return 3;
}
// main.c
#include <iostream>
#include "a.h"

using namespace std;

int main(void)
{
    A *a = new A();
    cout << a->test() << "\n";
    return 0;
}

// main.c
#include <iostream>
#include "a.h"

using namespace std;

int main(void)
{
    A *a = new A();
    cout << a->test() << "\n";
    return 0;
}
a->test()  
class A {
    public:
        A() {};
        virtual ~A() {};
//      ^^^^^^^
    //protected:
        virtual int test() {return 1;}
    private:
        friend class B;
};
#ifndef _A_
#define _A_

class A {
    ...
};

A* CreateB();

#endif
#include "a.h"
#include "b.h"

...

A* CreateB() {
    return new B();
}
#include <iostream>
#include "a.h"

using namespace std;

int main(void)
{
    A *a = CreateB();
    cout << a->test() << "\n";
    delete a;
    return 0;
}