我认为我的前向声明有问题。我认为一个是必要的,但我不确定。
基本上我有一个main.cpp:
//main.cpp
#include <iostream>
#include "CalculateForces.h"
#include "ParticleBox.h"
int main(void)
{
//g++ main.cpp ParticleBox.cpp -lgsl -lgslcblas -lm -std=c++0x
CalculateForces* calculate_forces= new CalculateForces();
ParticleBox* particles_box = new ParticleBox(2000,100,100,100);
delete calculate_forces;
delete particles_box;
return 0;
}
CalculateForces.h 看起来像这样:
//CalculateForces.h
//We update the forces on each particle
class ParticleBox;
class CalculateForces
{
public:
CalculateForces(void);
~CalculateForces(void);
int UpdateForces(ParticleBox* particlebox);
int DiscretizeSpace(float cutoff_distance);
int LJForce(int local_index, int remote_index, ParticleBox* particlebox);
};
最后 ParticleBox.h 文件如下所示:
//ParticleBox.h
//This is the definition of the particlebox. We manage all the particles in this
//file
//This should be changed to a template so that we can run float and double calcs properly :D
struct Particle;
class ParticleBox
{
public:
ParticleBox(int Num_Particles, float Box_length_x_, float Box_length_y_, float Box_length_z_);
~ParticleBox(void);
int set_num_particles(int Num_Particles);
int InitialiseUniverse(int temp,float mass);
float Boltzmann(float temperature);
int GenerateRandomUniquePositions(int number, float max, float min, float* rand_dim_positions);
private:
//Array to hold particles. Each particle has its own struct
Particle** particle_list_;
int num_particles_;
float box_length_x_;
float box_length_y_;
float box_length_z_;
float* rand_x_positions_;
float* rand_y_positions_;
float* rand_z_positions_;
float cutoff_distance_;
float sigma_;
float epsilon_;
};
int CalculateForces::DiscretizeSpace(float cutoff_distance, ParticleBox* particlebox)
{
......
return 0;
}
我在 Particle Struct 的 ParticleBox.h 中使用前向声明,并且可以将 Particle* 类型的指针添加到类中。这工作正常。
类 ParticleBox 的CalculateForces.h 中的转发会导致大量编译器错误(太多无法发布,但它们以与下面相同的方式开始)。省略它只会产生一些错误:
In file included from main.cpp:3:0:
CalculateForces.h:9:20: error: ‘ParticleBox’ has not been declared
CalculateForces.h:11:50: error: ‘ParticleBox’ has not been declared
In file included from CalculateForces.cpp:3:0:
CalculateForces.h:9:20: error: ‘ParticleBox’ has not been declared
CalculateForces.h:11:50: error: ‘ParticleBox’ has not been declared
CalculateForces.cpp:12:35: error: ‘int CalculateForces::UpdateForces’ is not a static member of ‘class CalculateForces’
CalculateForces.cpp:12:35: error: ‘ParticleBox’ was not declared in this scope
CalculateForces.cpp:12:48: error: ‘particlebox’ was not declared in this scope
CalculateForces.cpp:13:1: error: expected ‘,’ or ‘;’ before ‘{’ token
当我尝试将该类型用作参数时,我认为我需要前向声明?我究竟做错了什么?
感谢和抱歉,很长的帖子