我认为前向声明存在问题。我认为有必要,但是我不确定。

基本上我有一个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 *的指针。这很好。

ClassParticleBox的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

我以为在尝试使用该类型作为参数时需要前向声明?我究竟做错了什么?

谢谢,对不起,很长的帖子

最佳答案

您的帖子非常困惑,因为您发布了省略前向声明时显示的错误,并且显然您的代码中还有一些其他错误与所询问的错误混合在一起。

我假设有了前向声明,错误就会发生变化,因为它们主要出现在实现文件中,对吗?在这种情况下,问题可能是,只要声明一个指向该类型的指针,前向声明就足够了,但是当您开始使用指针(对其进行取消引用)时,这还不够。

如果是这种情况,则问题很可能是您忘记了#include "ParticleBox.h"(或某些其他实现文件)中的CalculateForces.cpp

07-24 14:13