c++: error: ' class '没有指定成员
C++: error: ‘class’ has no member named
对于我的毕业论文,我正在编写一些有限元代码,或者更准确地说,我正在修改一个现有的程序,该程序基于我的教师提供的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
定义为ConstVS
或BurgersMSrc
对象产生:
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;
}
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 助记符和指向成员语法的指针
- 用于访问容器<T>数据成员的正确 API
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 嵌套在类中时无法设置成员数据
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 将函数类成员映射到类本身内部
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 将Ref对象作为类成员
- 将包含C样式数组的对象初始化为成员变量(C++)
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 将公共但非静态的成员函数与ALGLIB集成
- 多成员Constexpr结构初始化
- 我们可以访问一个不存在的联盟的成员吗