为了我的毕业论文,我正在编写一些有限元程序,或者更准确地说,我正在修改一个现有的程序,它基于我的教员提供的两个类库。因此,我不能修改这些类,因为它们是通用的。
我已经创建了一个类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定义为ConstVS或BurgersMSrc对象会产生以下结果:
error: ‘srcTerm’ was not declared in this scope这也不是一种选择。
最后,我的问题是:
如果变量被定义为父类,但实例化为子类,那么如何访问子类的方法?
任何帮助都是非常感谢的,并提前感谢您的回答。
编辑
FYI,我在C++方面不是很有经验,但我在C#和VBA方面确实有一些编程经验。然而,我确实喜欢学习,所以指向正确方向的指针是非常受欢迎的:)
/Edit
头文件中的相关行:
#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;}
};
#endifCPP-文件中的相关行:
#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;
}发布于 2013-06-01 13:42:32
如果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
}发布于 2013-06-01 14:40:34
这可能对你的问题来说太过分了,但我想提出另一种解决方案。
在面向对象的程序中,必须在运行时使用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;
}https://stackoverflow.com/questions/16873312
复制相似问题