相关疑难解决方法(0)

在派生类中具有相同名称但签名不同的函数

我有一个具有相同名称的函数,但在基类和派生类中具有不同的签名.当我尝试在继承自派生的另一个类中使用基类的函数时,我收到一个错误.请参阅以下代码:

class A
{
    public:
    void foo(string s){};
};

class B : public A
{
    public:
    int foo(int i){};
};

class C : public B
{
    public:
    void bar()
    {
        string s;
        foo(s);
    }
};
Run Code Online (Sandbox Code Playgroud)

我从gcc编译器收到以下错误:

In member function `void C::bar()': no matching function for call to `C::foo(std::string&)' candidates are: int B::foo(int)
Run Code Online (Sandbox Code Playgroud)

如果我int foo(int i){};从课堂上删除B,或者我将其重命名foo1,一切正常.

这有什么问题?

c++ lookup inheritance function c++-faq

83
推荐指数
2
解决办法
4万
查看次数

为什么派生类不会访问非虚基类函数?

我无法使派生类访问基类中定义的函数.基类头文件名为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);
}
Run Code Online (Sandbox Code Playgroud)

该函数的定义位于名为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); …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance class

3
推荐指数
1
解决办法
1305
查看次数

标签 统计

c++ ×2

inheritance ×2

c++-faq ×1

class ×1

function ×1

lookup ×1