C++ 从另一个类调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14173217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Calling a function from another class
提问by CM99
Very new to c++ having trouble calling a function from another class.
C++ 的新手在调用另一个类的函数时遇到问题。
Class B inherites from Class A , and i want class A to be able to call a function created in class B.
B 类继承自 A 类,我希望 A 类能够调用在 B 类中创建的函数。
using namespace std;
class B;
class A
{
public:
void CallFunction ()
{
B b;
b.bFunction();
}
};
class B: public A
{
public:
virtual void bFunction()
{
//stuff done here
}
};
It all looks fine on screen (no obvious errors) but when I try to compile it i get an error C2079 'b' uses undefined class B.
屏幕上看起来一切正常(没有明显错误),但是当我尝试编译它时,出现错误 C2079 'b' 使用未定义的 B 类。
I've tried making them pointers/ friends
but i'm getting the same error,
我试过让他们成为指针/friends
但我遇到了同样的错误,
回答by nishantjr
void CallFunction ()
{ // <----- At this point the compiler knows
// nothing about the members of B.
B b;
b.bFunction();
}
This happens for the same reason that functions in C cannot call each other without at least one of them being declared as a function prototype.
发生这种情况的原因与 C 中的函数在没有至少其中一个被声明为函数原型的情况下无法相互调用的原因相同。
To fix this issue we need to make sure both classes are declared before they are used. We separate the declaration from the definition. This MSDN articleexplains in more detail about the declarations and definitions.
为了解决这个问题,我们需要确保两个类在使用之前都已声明。我们将声明与定义分开。这篇 MSDN 文章更详细地解释了声明和定义。
class A
{
public:
void CallFunction ();
};
class B: public A
{
public:
virtual void bFunction()
{ ... }
};
void A::CallFunction ()
{
B b;
b.bFunction();
}
回答by therealszaka
What you should do, is put CallFunction
into *.cpp file, where you include B.h.
您应该做的是放入CallFunction
*.cpp 文件中,其中包含 Bh
After edit, files will look like:
编辑后,文件将如下所示:
B.h:
乙:
#pragma once //or other specific to compiler...
using namespace std;
class A
{
public:
void CallFunction ();
};
class B: public A
{
public:
virtual void bFunction()
{
//stuff done here
}
};
B.cpp
B.cpp
#include "B.h"
void A::CallFunction(){
//use B object here...
}
Referencing to your explanation, that you have tried to change B b; into pointer- it would be okay, if you wouldn't use it in that same place. You can use pointer of undefined class(but declared), because ALL pointers have fixed byte size(4), so compiler doesn't have problems with that. But it knows nothing about the object they are pointing to(simply: knows the size/boundary, not the content).
参考你的解释,你试图改变B b; into 指针 - 如果您不在同一个地方使用它,那就没问题了。您可以使用未定义类(但已声明)的指针,因为所有指针都有固定的字节大小(4),因此编译器没有问题。但它对它们指向的对象一无所知(简单地说:知道大小/边界,而不是内容)。
So as long as you are using the knowledge, that all pointers are same size, you can use them anywhere. But if you want to use the object, they are pointing to, the class of this object must be already defined and known by compiler.
因此,只要您使用知识,即所有指针的大小都相同,您就可以在任何地方使用它们。但是如果你想使用这个对象,他们所指的是,这个对象的类必须已经被编译器定义和知道。
And last clarification: objects may differ in size, unlike pointers. Pointer is a number/index, which indicates the place in RAM, where something is stored(for example index: 0xf6a7b1).
最后澄清:与指针不同,对象的大小可能不同。指针是一个数字/索引,它表示 RAM 中存储内容的位置(例如索引:0xf6a7b1)。
回答by Anirudh Kumar
in A you have used a definition of B which is not given until then , that's why the compiler is giving error .
在 A 中,您使用了 B 的定义,直到那时才给出,这就是编译器给出错误的原因。
回答by Leonid Volnitsky
Forward declare class B
and swap order of A
and B
definitions: 1st B
and 2nd A
. You can not call methods of forward declared B
class.
前向声明class B
和交换A
和B
定义的顺序: 1stB
和 2nd A
。您不能调用前向声明B
类的方法。
回答by Hui Zheng
class B is only declaredbut not definedat the beginning, which is what the compiler complains about. The root cause is that in class A's Call Function, you are referencing instance b of type B
, which is incomplete and undefined. You can modify source like this without introducing new file(just for sake of simplicity, not recommended in practice):
class B 只是在开始时声明而不是定义,这是编译器抱怨的。根本原因是在类 A 的调用函数中,您引用了类型B
为不完整且未定义的实例 b 。您可以像这样修改源代码而不引入新文件(只是为了简单起见,实践中不推荐):
using namespace std;
class A
{
public:
void CallFunction ();
};
class B: public A
{
public:
virtual void bFunction()
{
//stuff done here
}
};
// postpone definition of CallFunction here
void A::CallFunction ()
{
B b;
b.bFunction();
}