問題(這個問題下面的答案)錯誤C2061:語法錯誤:標識符「機器人」
我知道有很多像我這樣的問題,但我沒有反正找到我的問題的解決方案。
I'w寫學校和一切小的項目是沒有問題跑,直到我說:#include "robot.h"
到interface.h和在初始化功能添加, Robot* _robot
interface.h。
我正在用Visual Studio 2013 Ultimate寫作它(我是學生;))。整個代碼是github訪問,而這些都是我寫的文件:
- allegroHelper.h的.cpp - 一些功能的易用性快板庫
- interface.h的.cpp - 類圖形界面
- logic.h的.cpp - 機器人的邏輯
- robot.h的.cpp - 機器人類
- 庫/ xkontiTextUtils.h的.cpp - 一些花哨控制檯的功能(某些被破壞)
- 庫/ xkontiVector2d.h .cpp - 小vec TOR類
是打破我的代碼的東西是存在的:
//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類是robot.h:
//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 ==========
我看了一下用於與#include
循環依賴但:
- 我在每一個報頭使用
#pragma once
- 我發現我包括interface.h在allegroHelper.h和allegroHelper.h在interface.h並且它長時間沒有問題地編譯。
你能告訴我我做錯了什麼嗎?或者任何提示要檢查什麼?
ANSWER
確定。我沒有太多時間,因爲我沒有找到任何方法在Interface中有指向Robot類的指針,我通過引用Robot類中的一些數據來訪問數據。
在主文件:
//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;
(...)
在Robot類:
//robot.h
(...)
class Robot {
public:
// Constuctor & Destructor
Robot(std::vector< std::vector<bool> >& _map, std::vector<Vector2D>& scanPoints);
~Robot();
(...)
std::vector< std::vector<bool> >& map;
std::vector<Vector2D>& scanPoints;
(...)
編譯器告訴你它不知道「Robot」是什麼。 「機器人」在哪裏定義? – Casey
我從來沒有使用過#pragma一次,而是在.h周圍創建了一個#ifndef,#define,#endif,每一個都獲得了不同的名稱。發佈您的robot.h – Samhain
剛添加robot.h到帖子。這不是分號@πάνταῥεῖ – Xkonti