Unrecognised type - 'Error: Variable “[var-nam

2019-06-28 04:33发布

I'm creating a class for handling physics as part of a project. We were told to use a class which handles arbitrary behaviours.

I've created a class which will update an internal state based on modules that are given to it (code follows). However, the structure representing the internal state, PhysicsData, isn't being recognised anywhere except its own file. Can anyone shed some light?

(Sorry for the massive dump of information, but the gap between the object being a problem and the place where it's a problem is quite large and trimming down the extra details also removes context that can be useful)

Here is the structure in question:

#pragma once

// This file "PhysicsBehaviourBase.h"

#include <d3dx9.h>
#include <vector>

struct PhysicsData
{
public:
    D3DXVECTOR3 velocity;
    D3DXVECTOR3 position;
    D3DXVECTOR3 rotation;
    float size;

    PhysicsData();
    void add(const PhysicsData& pd);
};

All references to PhysicsData in the rest of this file are fine. However this file starts to hint at problems:

#pragma once
// This file: "PhysicsBehaviours.h"

#include "PhysicsBehavioursBase.h"

class GravityConstant : public PhysicsBehaviour
{
private:
    float g; // Gravitational constant

    // Required by the PhysicsBehaviour interface.
    PhysicsBehaviour* copy() const {return new GravityConstant(g);}
public:
    GravityConstant(float accelleration_due_to_gravity = 9.81)
        : g(accelleration_due_to_gravity) {}

    // Required by the PhysicsBehaviour interface.
    void update(float time,const PhysicsData&, PhysicsData* out)
        {out->velocity.y -= g*time;}
};

In the void update(float time,const PhysicsData&, PhysicsData* out) line both references to PhysicsData are given the IntelliSense error message:

Physics PhysicsData 

Error: variable "PhysicsData" is not a type name.

I have no idea why IntelliSense think PhysicsData is a variable of type Physics. (Physics is a type I declare next, and PhysicsData is one of the parameters used to construct a physics object).

There is no compiler error at this point, however. The compiler error happens in the next file up the hierarchy:

#pragma once

// "Physics.h"

#include "Timing.h"
#include "PhysicsBehaviours.h"
#include <d3dx9.h> // For D3DXVECTOR3
#include <vector>

class Physics
{
private:
    std::vector<PhysicsBehaviour*> behaviours_;
    Timing timer;
    PhysicsData data;
    void addBehaviours(const BEHAVIOUR_LIST&);
public:
    Physics(const PhysicsData&,const BEHAVIOUR_LIST&);
    ~Physics() {}
    void update();
    PhysicsData state() const {return data;}
    float age() const {return timer.age();}
};

Both references to PhysicsData get the same IntelliSense error as above. Compiler errors point to this function:

#include "Physics.h"
// "Physics.cpp"

Physics(const PhysicsData& initalState,const BEHAVIOUR_LIST& behaviour) // Line 4
    : data(initialState) // Line 5
{ // Line 6
    addBehaviours(behaviour);
}

Compiler errors:

1>  Physics.cpp
1>[PATH]\physics.cpp(4): error C2226: syntax error : unexpected type 'PhysicsData'
1>[PATH]\physics.cpp(5): error C2065: 'initialState' : undeclared identifier
1>[PATH]\physics.cpp(6): error C2448: 'data' : function-style initializer appears to be a function definition

And more IntelliSense errors: Underneath the & in const PhysicsData& from line 4:

Error: this declaration has no storage class or type specifier.

Underneath the close bracket on line 4:

Error: expected a declaration.

Any clues, fixes or hypotheses are welcome.

1条回答
霸刀☆藐视天下
2楼-- · 2019-06-28 04:44

You're missing the class specifier from the constructor definition in Physics.cpp:

Physics::Physics(const PhysicsData& initalState,const BEHAVIOUR_LIST& behaviour)
^^^^^^^^^
查看更多
登录 后发表回答