错误C2061:语法错误:标识符'Robot'

时间:2014-01-23 20:49:26

标签: c++ visual-studio c++11

问题(在问题下方回答)

我知道有很多像我这样的问题,但无论如何我找不到解决问题的方法。

我正在为学校编写一个小项目,在我将<{1}}添加到 interface.h 并在 init中添加#include "robot.h"之前,所有内容都没有运行 interface.h 中的功能。

我正在使用Visual Studio 2013 Ultimate (我是学生;))编写它。整个代码可以在github访问,这些是我写的文件:

  • allegroHelper.h .cpp - 一些易于使用的函数allegro库
  • interface.h .cpp - 绘图界面的类
  • logic.h .cpp - 机器人的逻辑
  • robot.h .cpp - 机器人类
  • libs / xkontiTextUtils.h .cpp - 一些奇特的控制台功能(有些功能坏了)
  • libs / xkontiVector2d.h .cpp - 小矢量类

破坏我的代码的是:

, Robot* _robot

Robot类位于 robot.h

//interface.h
#pragma once


//////////////////////////////////////////
// INCLUDES
//////////////////////////////////////////

#include <string>
#include <vector>
#include "libs/xkontiTextUtils.h"
#include "libs/xkontiVector2d.h"
#include <allegro5/allegro.h>
#include "allegro5/allegro_image.h"
#include <allegro5/allegro_primitives.h>
#include "robot.h"
//#include "allegroHelper.h"    // Not needed due to forward declatation?


//////////////////////////////////////////
// INTERFACE CLASS
//////////////////////////////////////////

class Interface {
public:
    Interface();
    ~Interface();

    bool init(std::string _mapPath, int _width, XkontiConsoleColors* _con, Robot* _robot);
    (...)
    Robot* robot;
    (...)
};

我从编译器得到的错误:

//robot.h
#pragma once


//////////////////////////////////////////
// INCLUDES
//////////////////////////////////////////

#include <iostream>

#include <vector>
#include <math.h>
#include "libs/xkontiTextUtils.h"
#include "libs/xkontiVector2d.h"

#include <allegro5/allegro.h>
#include "allegro5/allegro_image.h"
#include <allegro5/allegro_primitives.h>

#include "allegroHelper.h"


//////////////////////////////////////////
// ROBOT CLASS
//////////////////////////////////////////

class Robot {
public:
    // Constuctor & Destructor
    Robot(std::vector< std::vector<bool> >& _map);
    ~Robot();

    // Initialization functions
    void initBody(Vector2D _size, Vector2D _pos, double _rotation, double _maxVelocity, double _maxAVelocity);
    void initHead(Vector2D _pos, Vector2D _rotRange, double _rangeMin, double _rangeMax, double _rangeError, unsigned int _resolution, double _rangeLess, double _rangeOver);

    // Set Functions
    void setPos(Vector2D _newPos);
    void setPos(double _x, double _y);
    void setRotation(double _rad);

    // Get Functions
    Vector2D getPos();
    Vector2D getHeadPos();
    double getRotation();
    int getStatus();

    // Commands Functions
    void move(double _dist);                // Move robot forward by specified distance
    void move(double _dist, double _rad);   // Move and turn robot
    void turn(double _rad);             // Turn robot by specified degree value
    std::vector<double>& scan();        // Scans terrain. Returns reference to vector of distances.

    // Periodical Functions
    void update(double dt);
    void draw(double dt);

    // Public Variables
    std::vector<Vector2D> scanPoints;

private:
    // Body functions

    // Head functions
    double trace(double _rad);          // Measure distance from current position

    // Outside pointers
    std::vector< std::vector<bool> >& map;

    // Body properties
    Vector2D size;      // Dimensions: Width, Length
    Vector2D pos;           // Position: X, Y
    double rotation;                    // Rotation: Z axis
    double leftDistance;                // Distance left to travel
    double leftRotation;                // Rotation left to rotate
    double maxVelocity;             // Max forward velocity
    double maxAVelocity;                // Max angular velocity on Z axis (left/right)

    // Head properties
    Vector2D headPos;           // Head position: X, Y
    Vector2D headRotRange;  // Head Z rotation range: from - to in deg
    double rangeMin;            // Minimum and Maximum detection range
    double rangeMax;
    double rangeError;          // Error percentage on range measuring
    unsigned int resolution;    // Number of traces in left/right scan
    double rangeLess;           // Number used when something was nearer than rangeMin
    double rangeOver;           // Number used when nothing was detected
};

我查找了1>------ Build started: Project: RoboSim, Configuration: Debug Win32 ------ 1> robot.cpp 1>d:\xkonti\github\robosim\interface.h(28): error C2061: syntax error : identifier 'Robot' 1>d:\xkonti\github\robosim\interface.h(39): error C2143: syntax error : missing ';' before '*' 1>d:\xkonti\github\robosim\interface.h(39): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1> logic.cpp 1>d:\xkonti\github\robosim\interface.h(28): error C2061: syntax error : identifier 'Robot' 1>d:\xkonti\github\robosim\interface.h(39): error C2143: syntax error : missing ';' before '*' 1>d:\xkonti\github\robosim\interface.h(39): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1> xkontiVector2d.cpp 1> Generating Code... 1> Skipping... (no relevant changes detected) 1> interface.cpp 1> allegroHelper.cpp 1> RoboSim.cpp ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 的循环依赖项,但是:

  1. 我在每个标题中使用#include
  2. 我发现 allegroHelper.h 中的 interface.h interface.h中 allegroHelper.h >它长时间没有问题地编译。
  3. 你能告诉我我做错了什么吗?或任何提示要检查的内容?

    ANSWER

    确定。我没有太多时间,因为我没有找到任何方法在Interface中有指向Robot类的指针,我通过引用Robot类中的一些数据来访问数据。

    在主文件中:

    #pragma once

    在Interface类中:

    //RoboSim.cpp
    std::vector< std::vector<bool> > map;
    std::vector<Vector2D> scanPoints;
    ALLEGRO_BITMAP* image = nullptr;
    (...)
    Interface inter = Interface(scanPoints);
    Robot robot = Robot(map, scanPoints);
    (...)
    

    在机器人课上:

    //interface.h
    (...)
    class Interface {
    public:
        Interface(std::vector<Vector2D>& _scanPoints);
        ~Interface();
        (...)
    private:
        XkontiConsoleColors* con;
        std::vector<Vector2D>& scanPoints;
        (...)
    

1 个答案:

答案 0 :(得分:2)

我不得不深入研究您的项目以查看问题。您通过allegroHelper.h在robot.h中包含interface.h,因此在您第一次看到它时未定义Robot

您可以向前宣告您正在做的很多事情,而不是使用#include

#pragma once


//////////////////////////////////////////
// INCLUDES
//////////////////////////////////////////

#include <string>
#include <vector>
#include "libs/xkontiTextUtils.h"
#include "libs/xkontiVector2d.h"
#include <allegro5/allegro.h>
#include "allegro5/allegro_image.h"
#include <allegro5/allegro_primitives.h>
// #include "robot.h"
#include "allegroHelper.h"

// the definition of Robot is not actually used, so just its name needs to be known here
class Robot;


//////////////////////////////////////////
// INTERFACE CLASS
//////////////////////////////////////////

class Interface {...};