C++ 中的类之间的数据重新循环 - 错误:'<class name>'未在此范围内声明
Data recirculation between classes in C++ - error: '<class name>' was not declared in this scope
我正在准备一个包含多个类的程序,这些类必须随后在代码中相互交换数据。这导致在每个其他标头中包含类。当将一个"Gas"对象的向量传递给"Thermal"类方法,然后尝试将一个更改后的"Thermal(对象传递给"Gas("类方法时,我遇到了一个问题。我尝试过将类包含在其他头文件中,但结果很糟糕。也许我做错了什么。类对象之间的指针声明可能会解决问题吗?我已经厌倦了这一点,但不幸的是,我可能缺乏指针经验,因为我也失败了。
注意-当我注释掉"Gas"类中的#include"Thermal.h"时,代码编译成功。然而,扩散方法也必须被注释掉。
compilier返回如下:
./src/Thermal.h:86:76: error: template argument 2 is invalid
./src/solution.cpp: In function 'void solution(const Ref::Reformer&,
const std::vector<Ref::Segment>&, std::ofstream*, std::ofstream*, std::ofstream*)':
./src/solution.cpp:92:40: error: no matching function for call to 'Ref::Thermal::
conduction(const Ref::Reformer&, Ref::Grid&, std::vector<Ref::Gas>&, Ref::Velocity&)'
T.conduction(reactor, grid, gases, vel);
^
In file included from ./src/Gas.h:20:0,
from ./src/solution.cpp:16:
./src/Thermal.h:86:8: note: candidate: void Ref::Thermal::conduction(const Ref::Reformer&,
const Ref::Grid&, const int&, const Ref::Velocity&)
void conduction(const Reformer& RE, const Grid& GD, const std::vector<Gas>& GAS,
const Velocity& VE);
^
./src/Thermal.h:86:8: note: no known conversion for argument
3 from 'std::vector<Ref::Gas>' to 'const int&'
以下是我如何调用这些方法。类"Thermal"answers"Gas"的对象在同一.cpp文件中创建,随后调用。它们的初始化需要构造和传递一个"Grid"类对象,这也在同一个文件中完成。
对象创建:
Grid grid(0.3, 0.052);
grid.setNX(90);
grid.setNR(15);
Thermal T(grid);
for(i = 0; i < 6; i++){
gases.push_back(Gas(i, grid));
}
方法调用:
T.conduction(reactor, grid, gases, vel);
for(int i = 0; i < gases.size(); i++){
gases[i].diffusion(reactor, grid, vel, T);
}
网格类声明:
#ifndef REFORMING_CODE_REF_GRID_H
#define REFORMING_CODE_REF_GRID_H
#include "../input.h"
namespace Ref{
class Grid{
public:
Grid(const double& x1, const double& r1, const double& x0 = 0., const double& r0 = 0.){
xmin_ = x0;
xmax_ = x1;
rmin_ = r0;
rmax_ = r1;
}
void setNX(const int& nx){ //setting number of elements in the longitudinal direction
NX_ = nx;
ni_ = NX_ - 1;
}
void setNR(const int& nr){ //setting number of elements in the radial direction
NR_ = nr;
nj_ = NR_ - 1;
}
};
}//end of namespace
#endif //REFORMING_CODE_REF_GRID_H
热等级声明:
#ifndef REFORMING_CODE_REF_THERMAL_H
#define REFORMING_CODE_REF_THERMAL_H
#include "../input.h"
#include "Reformer.h"
#include "Grid.h"
#include "Velocity.h"
#include "Gas.h"
namespace Ref{
class Thermal{
public:
Thermal(const Grid& grid){
NX_ = grid.NX();
NR_ = grid.NR();
}
void conduction(const Reformer& RE, const Grid& GD, const std::vector<Gas>& GAS, const Velocity& VE);
private:
int NX_; //quantity of elements in the X direction
int NR_; //quantity of elements in the R direction
std::vector<double> val_; //Temperature value (K)
std::vector<double> val_old_;
std::vector<double> s_; //Thermal source
};
} //end of namespace
#endif //REFORMING_CODE_REF_THERMAL_H
传导方法定义:
#include "Thermal.h"
namespace Ref{
void Thermal::conduction(const Reformer& RE, const Grid& GD,
const std::vector<Gas>& GAS, const Velocity& VE){}
}
气体等级声明:
#ifndef REFORMING_CODE_REF_GAS_H
#define REFORMING_CODE_REF_GAS_H
#include "../input.h"
#include "Reformer.h"
#include "Grid.h"
#include "Velocity.h"
#include "Thermal.h"
namespace Ref{
class Gas{
public:
Gas(const int id, const Grid& grid){
id_ = id;
NX_ = grid.NX();
NR_ = grid.NR();
}
void diffusion(const Reformer&, const Grid&, const Velocity&, const Thermal&);
private:
int id_;
int NX_;
int NR_;
};
} //end of namespace
#endif //REFORMING_CODE_REF_GAS_H
扩散方法定义:
#include "Gas.h"
namespace Ref{
void Gas::diffusion(const Reformer& RE, const Grid& GD,
const Velocity& VE, const Thermal& T){}
} //end of namespace
您可以尝试使用正向声明并将#include
移动到标头的末尾。(由于标头保护,您的文件不会被包含两次(
最小编译示例:
//RecursiveA.h
#ifndef RECURSIVEA_H
#define RECURSIVEA_H
class RecursiveA {
public:
void workOnB(const class RecursiveB &);
};
#include "RecursiveB.h"
#endif // RECURSIVEA_H
//RecursiveB.h
#ifndef RECURSIVEB_H
#define RECURSIVEB_H
class RecursiveB {
public:
void workOnA(const class RecursiveA &);
};
#include "RecursiveA.h"
#endif // RECURSIVEB_H
//RecursiveA.cpp
#include "RecursiveA.h"
void RecursiveA::workOnB(const RecursiveB &b){
}
//RecursiveB.cpp
#include "RecursiveB.h"
void RecursiveB::workOnA(const RecursiveA &a){
}
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- C++核心准则 C35 对于接口类"A base class destructor should be either public and virtual, or protected and nonv
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- <<操作员在下面的行中工作
- "Undefined class"作为is_base_of的论据
- 如何将class.cpp和class.hpp编译为一个.o文件
- '{'标记之前的预期类名,然后在预声明时无效使用不完整的类型'class class_name'
- 如何使用"derived input class"创建派生类?
- 错误:"Left of getValue must have class/struct/union"
- ' class a : b ' 和 ' class a : public b ' 之间的继承类不同
- Pybind11 Class Definition
- 在C++中使用没有合作伙伴Class/Cpp文件的头文件是否实用
- C2011 'CMemDC':Visual Studio 2019中的'class'类型重新定义
- TypeError: [c++ addon class] 不是构造函数
- C++ class template
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- Clearing Class Foo with new(pFoo) Foo()