C++ 为什么赢了';派生类是否访问非虚拟基类函数?

C++ 为什么赢了';派生类是否访问非虚拟基类函数?,c++,class,inheritance,C++,Class,Inheritance,我在使派生类访问基类中定义的函数时遇到问题。名为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 functi

我在使派生类访问基类中定义的函数时遇到问题。名为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);
}
类粒子{
受保护的:
向量x,y,z;
//许多与该问题无关的其他受保护成员
公众:
//省略了与问题无关的构造函数和其他函数
虚拟双getX(常数无符号整数索引、常数双θ、常数双φ);
虚向量getX(常数双θ,双实φ);
向量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;
}
#包括“Particle.h”
向量粒子::getX(常量无符号整数surfindex)
{
向量xp;
xp.clear();
xp.调整大小(3,0.0);
xp[0]=x_u2;at(surfindex);
xp[1]=y_uu.at(surfindex);
xp[2]=z_u.at(surfindex);
返回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);
}
#包括“Particle.h”
使用名称空间std;
星级:公共粒子{
公众:
//这里省略了构造函数和其他与问题无关的函数
虚空计算机健全度(常数无符号整数法);
double getX(常数无符号整数索引、常数双θ、常数双φ);
向量getX(常数双θ,双实φ);
}
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.h”
//这里省略了许多与问题无关的其他函数定义
void Star::computeRoundness(const unsigned int方法)
{
向量X;
无符号整数计数=0;
同时(计数
但我收到这个编译时错误:

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)
Star.cc:在成员函数“virtual void Star::computeRoundness(unsigned int)”中:
cc:1340:错误:调用'Star::getX(unsigned int&')没有匹配的函数
Star.h:687:注意:候选项是:虚拟双星::getX(unsigned int,double,double)
Star.h:696:note:virtualstd::vector Star::getX(double,double)

我曾经成功地从其他C++项目中的派生类中调用基类函数,所以我必须忽略这里的一些简单的东西,但我就是找不到。我认为基类函数应该由派生类继承,除非它们被声明为虚函数,然后在派生类中被重写(但这里不是这种情况),即使派生类像我在这里多次做的那样重载函数名。那不是真的吗?如果没有,除了在派生类中重新定义相同的函数,我还能做些什么来解决这个问题呢


非常感谢您的帮助。

您必须使用getX添加
,或在函数中使用
particle::getX

该标准规定,如果派生函数具有相同名称的函数,则不会自动使用基类函数,即使派生函数不适合。这是为了防止虫子


您必须告诉派生类它将使用基类函数(通过使用getX;
direvive)或显式调用基类函数(通过调用
particle::getX(…)

基类中函数getX的声明是:

virtual vector<double> getX(const double theta, double Real phi);
虚拟向量getX(常数双θ,双实φ);
但是的定义是:

vector<double> Particle::getX(const unsigned int surfindex)
{
    //Stuff
}
向量粒子::getX(常量无符号整数surfindex) { //东西 }
签名不匹配!更改声明,代码将起作用

一个向量的拷贝非常昂贵,考虑使用参考文献

来改变你的设计。答案是: 如果基类有“n”个重载版本的函数 和 在派生类中,重新定义/重写(更改主体)或重载(更改签名)该函数,则只有派生类中的该版本对您可用

样本:

class Base
{
    public:
    int abc(){}
    float abc(){}
};

class Der:public Base
{
    public:
    SomeClassObj abc(){} //int and float returning versions of abc are now hidden for Der object
};

重写/重新定义的情况也是如此。

我想问一个类似的问题。这是否意味着如果我有同名的重载/重写函数,我需要重新复制派生类中每个未更改的基类函数?这似乎有可能迫使大量代码重复。这是我设计拙劣的表现吗?