【问题标题】:Header File Not Seeing Other Header Identifier头文件看不到其他头标识符
【发布时间】:2014-12-22 21:50:26
【问题描述】:

我在使用 Visual Studio 2013 时遇到问题。我无法解释为什么会这样。编译问题如下图所示。
我有一个使用“RigidBody.cpp/h”类的“PhysicsEngine.cpp/h”类就好了。一旦我尝试在我的 Character 类标头中使用 RigidBody 类,它就无法看到 RigidBody 类标识符。我已经在我的文件中包含了“RigidBody.h”,甚至尝试对这个类进行原型设计。有趣的是,当我删除“RigidBody.h”标题包含时,它会吐出更多错误,让我相信它正在阅读它。是不是我做错了什么?

这是我收到的错误输出:

1>------ Rebuild All started: Project: SideScroller, Configuration: Debug Win32 ------
1>  WorldChunkManager.cpp
1>  World.cpp
1>  Vector.cpp
1>e:(directory)\vector.cpp(41): warning C4244: 'argument' : conversion from 'double' to 'float', possible loss of data
1>e:(directory)\vector.cpp(48): warning C4244: 'argument' : conversion from 'double' to 'float', possible loss of data
1>  Tile.cpp
1>  RigidBody.cpp
1>e:(directory)\character.h(21): error C2146: syntax error : missing ';' before identifier 'pos'
1>e:(directory)\character.h(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:(directory)\character.h(21): error C2143: syntax error : missing ';' before '='
1>e:(directory)\character.h(21): error C2238: unexpected token(s) preceding ';'

这是 RigidBody.h 的代码

#ifndef RH
#define RH

#include "Vector.h"
#include "World.h"
#include <cmath>
#include <list>

class RigidBody
{
    private:
    int id;
    float gravityFactor;
    float airResistFactor;

    float mass;
    float elasticity;

    Vector pos = Vector(0, 0);
    int width;
    int height;

    Vector velocity = Vector(0, 0);

    std::list<Vector>* additiveVelocities;
    std::list<Vector>* forces;
public:
    RigidBody(int x, int y, int w, int h, float mass, float elasticity, float gravityFactor,         float airResistFactor);
    ~RigidBody();

    static int rigidBodyCount;

    //Get/Set
    int getID();
    double getX();
    double getY();
    float getGravFact();
    float getAirFact();
    float getMass();
    float getElast();
    Vector getPos();
    Vector getVelocity();
    std::list<Vector>* getadditiveVelocities();
    std::list<Vector>* getForces();

    void setX(double x);
    void setY(double y);
    void setVelocity(Vector& vec);
    void setPos(Vector& vec);

    //Interactino Functions
    void addForce(Vector force);
    void addVelocity(Vector velocity);
};


#endif

这是不起作用的代码(Character.h):

#ifndef CHARACTER_H
#define CHARACTER_H

#include "SDL.h"
#include "Camera.h"
#include "InputManager.h"
#include "Vector.h"
#include "RigidBody.h"

//TEMP DIM
const int CHAR_H = 64;
const int CHAR_W = 12;

class Character
{
private:
    double speed;

    //Vector pos = Vector(0, 0);

    RigidBody pos = RigidBody(0, 0, CHAR_W, CHAR_H, 50, 0, 1, 1);

    InputManager* inputPtr;
public:
    Character(int x, int y, InputManager* inputPtr);

    void getXY(int* dest);
    void getChXY(int* dest);
    void getCenterXY(int* dest);

    //Update
    void update(long double last);

    bool isFloor();

    //Draw
    void draw(SDL_Surface* screen, Camera* camPtr);
};

#endif

如果您还需要其他任何东西来查找问题,请告诉我!!!我不想用可能不敬的信息使页面超载。谢谢!

【问题讨论】:

    标签: c++ visual-studio compiler-errors header include


    【解决方案1】:

    我认为这一行的问题不在于找不到标识符:

    RigidBody pos = RigidBody(0, 0, CHAR_W, CHAR_H, 50, 0, 1, 1);
    

    相反,您正在尝试以 C# 类型的方式进行非法初始化。您不能像在 C++ 中那样将值分配给类成员作为其声明的一部分。您需要在构造函数中或附加到构造函数的初始化程序中分配初始值。

    【讨论】:

    • 我试图改变它,但它仍然给出了同样的错误。我明白你为什么这么说吗?我将标题更改为“RigidBody* pos;”然后在 Character.cpp 构造函数中输入“pos = new RigidBody(0, 0, CHAR_W, CHAR_H, 50, 0, 1, 1);"。
    • 好吧,好吧,如果你要把它改成指针类型,那么你可以在头文件中使用前向声明,即class RigidBody;,然后#include必要的头文件.cpp 文件。
    • 我只是预处理文件并查看编译器实际查看的内容:stackoverflow.com/questions/8978997/…
    • 成功了,谢谢!!只是好奇。您知道为什么放置#include 不起作用但前向声明起作用吗?所以我以后不会遇到类似的问题了。
    • 使用原力,我的意思是预处理器。
    猜你喜欢
    • 2023-03-06
    • 2015-01-11
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多