- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我无法让派生类访问基类中定义的函数。名为 Particle.h 的基类头文件是:
class Particle {
protected:
vector<double> x_,y_,z_;
// lots of other protected members that are not relevant to the question
public:
// constructors and other functions not relevant to question are omitted
virtual double getX(const unsigned int index, const double theta, const double phi);
virtual vector<double> getX(const double theta, double Real phi);
vector<double> getX(const unsigned int surfindex);
}
此函数的定义在名为 Particle.cc 的文件中:
#include "Particle.h"
vector<double> Particle::getX(const unsigned int surfindex)
{
vector<double> xp;
xp.clear();
xp.resize(3,0.0);
xp[0] = x_.at(surfindex);
xp[1] = y_.at(surfindex);
xp[2] = z_.at(surfindex);
return xp;
}
派生类头文件,称为Star.h,是:
#include "Particle.h"
using namespace std;
class Star : public Particle {
public:
// constructors and other functions not relevant to question are omitted here
virtual void computeRoundness(const unsigned int method);
double getX(const unsigned int index, const double theta, const double phi);
vector<double> getX(const double theta, double Real phi);
}
computeRoundness 函数的定义在一个名为 Star.cc 的文件中:
#include "Star.h"
// Lots of other function definitions not relevant to question are omitted here
void Star::computeRoundness(const unsigned int method)
{
vector<double> X;
unsigned int count = 0;
while (count < ntheta) {
X = getX(count); // Should call base class function, right?
// do some more things with X that are not relevant to this question
}
}
但是我收到这个编译时错误:
Star.cc: In member function ‘virtual void Star::computeRoundness(unsigned int)’:
Star.cc:1340: error: no matching function for call to ‘Star::getX(unsigned int&)’
Star.h:687: note: candidates are: virtual double Star::getX(unsigned int, double, double)
Star.h:696: note: virtual std::vector<double, std::allocator<double> > Star::getX(double, double)
我过去在其他 C++ 项目中成功地从派生类中调用了基类函数,所以我一定是在这里忽略了一些简单的东西,但我就是找不到它。我认为基类函数应该由派生类继承,除非它们被声明为虚拟然后在派生类中被覆盖(但这里不是这种情况),即使派生类像我在这里所做的那样重载了函数名次。这不是真的吗?如果没有,我能做些什么优雅的事情来解决这个问题,而不是仅仅在我的派生类中重新定义相同的函数?
非常感谢您的帮助。
最佳答案
您必须在派生类中添加 using getX;
,或者在函数中使用 particle::getX
。
标准规定,如果派生类具有同名函数,您不会自动使用基类的函数——即使派生类的函数不适合。这是为了防止错误。
您必须告诉派生类它将使用基类的函数(通过 using getX;
指令)或显式调用基类的函数(通过调用 particle::getX(...)
)
关于c++ - 为什么派生类不能访问非虚拟基类函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19038568/
经过几个小时的(重新)搜索,我无法想出普通抽象类和使用模板模式之间的可解释区别。 我唯一看到的是: 使用抽象类时,您需要实现所有方法。但是在使用模板方法时,您只需要实现这两个抽象方法。 有人可以向我解
我正在尝试实现一种算法,该算法可找到以下形状给出的外多边形的每个单独边的对应区域。也就是说,1,2 边的相应区域是 [1,6,7,8,2],2,3 边的区域是 [2,8,3] 等等,CCW 或 CW
我正在尝试在派生 self 的 BaseController 类的任何 Controller 上自动设置一个属性。这是我的 Application_Start 方法中的代码。 UnitOfWork 属
我正在使用 mgcv 包通过以下方式将一些多项式样条拟合到一些数据: x.gam smooth$knots [1] -0.081161 -0.054107 -0.027053 0.000001
考虑以下代码: void foo(){ ..... } int main() { int arr[3][3] ; char string[10]; foo();
本书The c++ programming language有这个代码: class BB_ival_slider : public Ival_slider, protected BBslider {
是否有一个 package.json 属性可用于指定模块解析应启动的根文件夹? 例如,假设我们在 node_modules/mypackage/src/file1 中有一个安装。我们要导入的所有文件都
我正在尝试使用聚合函数来实现与 SQL 查询相同的结果: 查询语句: sqldf(" SELECT PhotoID, UserID,
我正在比较使用 LOESS 回归的两条线。我想清楚地显示两条线的置信区间,我遇到了一些困难。 我尝试过使用各种线型和颜色,但在我看来,结果仍然是忙碌和凌乱。我认为置信区间之间的阴影可能会使事情变得更清
给定这段代码 public override void Serialize(BaseContentObject obj) { string file = ObjectDataStoreFold
我正在构建某种工厂方法,它按以下方式将 DerivedClass 作为 BaseClass 返回: BaseClass Factory() { return DerivedClass(); }
当重写 class delegation 实现的接口(interface)方法时,是否可以调用通常从重写函数中委托(delegate)给的类?类似于使用继承时调用 super 的方式。 来自docum
我有一个基类 fragment (如下所示)。我在其他 3 个 fragment 类中扩展了此类,每个类都共享需要在这 3 个 fragment 中访问的相同 EditText。因此,我在基类中设置了
如何在不加载额外库的情况下在 R 中计算两个排列之间的 Kendall tau 距离(又名冒泡排序距离)? 最佳答案 这是一个 O(n.log(n)) 的实现,在阅读后拼凑而成,但我怀疑可能有更好的
情况 我创建了一个具有国际化 (i18n) 的 Angular 应用程序。我想在子域中托管不同的版本,例如: zh.myexample.com es.myexample.com 问题 当我使用命令 n
std::is_base_of 之间的唯一区别和 std::is_convertible是前者在 Base 时也成立是 私有(private)或 protected Derived 的基类.但是,您何
我创建了一个名为 baseviewcontroller 的父类(super class) uiviewcontroller 类,用于包含大多数应用屏幕所需的基本 UI。它包括一个自定义导航栏和一个“自
我是一名优秀的程序员,十分优秀!