C++ 中的类之间的数据重新循环 - 错误:'<class name>'未在此范围内声明

Data recirculation between classes in C++ - error: '<class name>' was not declared in this scope

本文关键字:class lt name 范围内 声明 gt 错误 数据 之间 新循环 循环      更新时间:2023-10-16

我正在准备一个包含多个类的程序,这些类必须随后在代码中相互交换数据。这导致在每个其他标头中包含类。当将一个"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){
}