前方声明问题

时间:2013-03-08 17:28:56

标签: c++ forward-declaration

我认为我在使用前向声明时遇到了问题。我认为有必要,但我不确定。

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

}

我在粒子结构的ParticleBox.h中使用前向声明,我可以在类中添加粒子类型的指针。这很好。

类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

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

感谢并且抱歉这篇长篇文章

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

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