简体   繁体   English

如何调用虚虚函数C ++?

[英]How to call a virtual void function C++?

I came across an exercise that I found online. 我在网上发现了一个练习。 This exercise consist of "virtual void" that I never seen before of how it works. 这项练习由“虚拟虚空”组成,我从没见过它是如何工作的。 So the exercise contain 1 header file named plans.h 因此,该练习包含1个名为plans.h的头文件。

namespace plans {

class HumanActions {
public:
  virtual void goTo() { }
  virtual void haveANiceColdBeer() { }
};

void applyPlan(HumanActions &actions);

}

and one cpp file 和一个cpp文件

#include "Plans.h"

using plans::HumanActions;
using plans::applyPlan;

void
plans::applyPlan(HumanActions &actions) {
  actions.goTo();
  actions.haveANiceColdBeer();
}

I tried to run the function by having another main file that runs like 我试图通过运行另一个主文件来运行该功能

#include "Plans.h"

using plans::HumanActions;
using plans::applyPlan;

int main() {
    HumanActions actions;
    applyPlan(actions);
}

Unfortunately it does not run and it says that I have undefined reference to `plans::applyPlan(plans::Actions&)' so My question is how do you pass the functions for those argument given? 不幸的是它没有运行,并且说我对“ plans :: applyPlan(plans :: Actions&)”有未定义的引用,所以我的问题是如何传递给定参数的函数?

You have declared "void applyPlan(HumanActions &actions)" in the header file, but you have not defined (implemented) it anywhere, which is why you have the undefined symbol. 您已经在头文件中声明了“ void applyPlan(HumanActions&actions)”,但尚未在任何位置定义(实现)它,这就是为什么您有未定义符号的原因。

If in main(), you called applyZombiePlan() instead of applyPlan() you should be OK. 如果在main()中,您调用了applyZombiePlan()而不是applyPlan(),则应该没问题。

"Undefined reference" is a linker error that means the implementation for a function couldn't be found. “未定义的引用”是一个链接器错误,表示找不到函数的实现。 In other words, it typically means you declared the function, and called the function, but forgot to actually write the function. 换句话说,这通常意味着您声明了该函数并调用了该函数,但是却忘记了实际编写该函数。

Your error message refers to plans::applyPlan(plans::Actions&) , but the closest declaration I see is void applyPlan(HumanActions &actions); 您的错误消息引用了plans::applyPlan(plans::Actions&) ,但是我看到的最接近的声明是void applyPlan(HumanActions &actions); , which has a different argument type. ,具有不同的参数类型。 Assuming this isn't just a mix-up between different versions of the code in your post, you might have accidentally declared two different applyPlan functions but only implemented one of them. 假设这不仅是帖子中不同版本的代码之间的混淆,您可能不小心声明了两个不同的applyPlan函数,但只实现了其中一个。

If your program consists of more than one .cpp file, another possible reason is that you're accidentally trying to compile and link a single file as if it were a complete program instead of linking all the modules together. 如果您的程序包含多个.cpp文件,则另一个可能的原因是您无意间试图像编译一个完整程序一样编译和链接一个文件,而不是将所有模块链接在一起。 That'd make the linker not see anything defined in the other .cpp files. 这会使链接器看不到其他.cpp文件中定义的任何内容。 If you're a beginner, your best bet is to compile all your source files at once with a single command line, eg g++ *.cpp -o myprog . 如果您是初学者,最好的选择是使用单个命令行一次编译所有源文件,例如g++ *.cpp -o myprog

Calling a virtual function is no different from calling a regular function, BTW, and applyPlan is not a virtual function anyway. 调用虚拟函数与调用常规函数BTW没什么不同, applyPlan也不是虚拟函数。

I think you can make use of extern keyword. 我认为您可以使用extern关键字。 whenever you are actually defining your global function, you can say void XYZ(){} and everywhere else you can say extern void XYZ(). 每当您实际定义全局函数时,都可以说void XYZ(){},而其他任何地方都可以说extern void XYZ()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM