2

我认为我的前向声明有问题。我认为一个是必要的,但我不确定。

基本上我有一个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

当我尝试将该类型用作参数时,我认为我需要前向声明?我究竟做错了什么?

感谢和抱歉,很长的帖子

4

2 回答 2

1

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

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

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

于 2013-03-08T18:10:03.940 回答
0

Rob 确定错误的根源在于我没有发布的一些代码。有一些错误,但最大的是在CalculateForces.cpp中我试图访问int num_particles_;它当然是ParticleBox的私有成员。

于 2013-03-09T14:32:53.933 回答