c++: error: ' class '没有指定成员

C++: error: ‘class’ has no member named

本文关键字:成员 c++ class error      更新时间:2023-10-16

对于我的毕业论文,我正在编写一些有限元代码,或者更准确地说,我正在修改一个现有的程序,该程序基于我的教师提供的2个类库。因此,我不能修改这些类,因为它们是通用的。

我创建了一个类BurgersMSrc,它继承了父类ValSrc。我用calcFourierCoefficient方法扩展了子类。在编译期间,我得到以下错误:

burgers1d.cpp:268:12: error: ‘class ValSrc’ has no member named ‘calcFourierCoefficient’
这是有意义的,因为变量被定义为:
ValSrc* srcTerm;

,它没有定义方法。稍后将变量实例化为

srcTerm = new ConstVS(f);

srcTerm = new BurgersMSrc(prm);

,其中实例化取决于问题类型。有条件地将srcTerm定义为ConstVSBurgersMSrc对象产生:

error: ‘srcTerm’ was not declared in this scope

这也不是一个选项。

最后我的问题是:

如果一个变量被定义为父类,但作为子类实例化,我如何访问子类的方法?

任何帮助都是非常感激的,并提前感谢您的回答。

编辑

仅供参考,我对c++不是很有经验,但我在c#和VBA方面有一些编程经验。但是我确实喜欢学习,所以我非常欢迎你给我指路:)

/编辑

头文件中的相关行:

#ifndef BurgersMSS_H
#define BurgersMSS_H
#include "mfem.hpp"
#include "mex.h"
class BurgersMSol: public ValSrc
{
   ...
};
class BurgersMSrc: public ValSrc
{
public:
    typedef ValSrc Super;
    BurgersMSrc(ParamDB &prm) {init(prm);}
    virtual void init(ParamDB &prm);
    ~BurgersMSrc(){}
    inline void getValues  (Vector &coords, Vector &msrc){}
    void calcFourierCoefficient(int p){}
private:
    double   nu;
    double   Tn;
    int prob;
    int nTimeSteps;
    int specModes;
    double s_n;
    double tT;
    double deltaT;
    vector <double> a_re;
    vector <double> a_im;
    int accuracy;
    double randomNr;
    double randomNumber(int p){return randomNr;}
};
#endif

cpp文件中的相关行:

#include "BurgersMSS.h"
void BurgersMSol::init(ParamDB &prm) 
{ 
    ...
}
BurgersMSol::~BurgersMSol(){}
inline void BurgersMSol::getValues (Vector &coords, Vector &msol) 
{
    ...
}
BurgersMSrc::init(ParamDB &prm) 
{ 
    Super::init(); objectName="BurgersMSrc";
    nu = 1.0; prm.find("nu", nu);
    prob = 1; prm.find("problem", prob);
    if (prob == 3)
    {
        ...
        this->calcFourierCoefficient(accuracy);
    }
}
BurgersMSrc::~BurgersMSrc(){}
inline void BurgersMSrc::getValues  (Vector &coords, Vector &msrc)
{
    ...
}
void BurgersMSrc::calcFourierCoefficient(int p)
{   
    for(int n=0;n<specModes;n++)
    {
        if (time == 0)
        {
            a_re[n] = randomNumber(p);
            a_im[n] = randomNumber(p);
        }
        else
        {
            a_re[n] = a_re[n]*exp(-tT) + randomNumber(p);
            a_im[n] = a_im[n]*exp(-tT) + randomNumber(p);
        }
    }
}
double BurgersMSrc::randomNumber(int p)
{
    int mod = pow(10,p);    
    int rN = -mod + rand() % (2*mod);
    randomNr = rN/(double)mod;
    return randomNr;
}

主程序的相关行:

#include "mfem.hpp"
#include "mex.h"
#include "BurgersMSS.h"
...
int main (int argc, char *argv[]) {
    ...
    ValSrc *srcTerm;
    ...
    if (problem==1) {
        ... srcTerm = new ConstVS(f);
        ...
    } else if (problem==2) {
        ... srcTerm  = new ConstVS(f);
        ...
    } else if (problem==3){
        srcTerm = new BurgersMSrc(prm); 
        ...
    } else {
        srcTerm = new BurgersMSrc(prm); 
        ...
    }
    ...
    stiffInt->setSrc(*srcTerm);
    ...
    for (int step = 0; step < nTimeSteps; step ++) {
    ...
        if (problem == 3)
        {
            srcTerm->calcFourierCoefficient(accuracy); //This line throws the error
        }
        ...
    }
    ...
    return 0;
}

如果ValSrc没有方法calcFourierCoefficient,那么你不能在指向ValSrc的指针上调用该方法。您必须转换为适当的类型。例如:

BurgersMSrc* p = dynamic_cast<BurgersMSrc*>(srcTerm);
if (p)
{
  p->calcFourierCoefficient(accuracy);
} else
{
  // srcTerm was not pointing to an instance of the appropriate type
}

对于您的问题,这可能有点过分了,但我想提出另一种解决方案。

在面向对象的程序中,必须使用dynamic_cast或类似的技术在运行时确定对象的类型,然后执行一些特定于类型的逻辑,通常被认为是设计问题的症状。下面是另一种方法:

struct Problem {
    virtual ValSrc &valSrc() = 0;
    virtual void doStep() = 0;
    void main(StiffInt *stiffInt);
};
void Problem::main(StiffInt *stiffInt)
{
    // ...
    stiffInt->setSrc(valSrc());
    // ...
    for (int step = 0; step < nTimeSteps; step ++) {
        // ...
        doStep();
        // ...
    }
    // ...
}
struct Problem1 : Problem {
    ConstVS srcTerm;
    Problem1(F f) : srcTerm(f)
    {
        // ...
    }
    virtual ValSrc &valSrc() { return srcTerm; }
    virtual void doStep()
    {
        // ...
    }
};
struct Problem2 : Problem {
    ConstVS srcTerm;
    Problem2(F f) : srcTerm(f)
    {
        // ...
    }
    virtual ValSrc &valSrc() { return srcTerm; }
    virtual void doStep()
    {
        // ...
    }
};
struct Problem3 : Problem {
    BurgersMSrc srcTerm;
    Problem3(PRM prm) : srcTerm(prm)
    {
        // ...
    }
    virtual ValSrc &valSrc() { return srcTerm; }
    virtual void doStep()
    {
        srcTerm.calcFourierCoefficient(accuracy);
    }
};
struct Problem4 : Problem {
    BurgersMSrc srcTerm;
    Problem4(PRM prm) : srcTerm(prm)
    {
        // ...
    }
    virtual ValSrc &valSrc() { return srcTerm; }
    virtual void doStep()
    {
        // ...
    }
};
int main (int argc, char *argv[]) {
    // ...
    if (problem==1) {
        Problem1(f).main(stiffInt);
    } else if (problem==2) {
        Problem2(f).main(stiffInt);
    } else if (problem==3){
        Problem3(prm).main(stiffInt);
    } else {
        Problem4(prm).main(stiffInt);
    }
    return 0;
}