Xkonti
Xkonti

Reputation: 119

error C2061: syntax error : identifier 'Robot'

QUESTION (answer below the question)

I know there's lot of questions like mine, but I didn't find the solution for my problem anyway.

I'w writing small project for school and everything was running with no problem until I added: #include "robot.h" into interface.h and added , Robot* _robot in init function in interface.h.

I'm writing it with Visual Studio 2013 Ultimate (I'm a student ;) ). The whole code is accessible on github and those are files written by me:

The thing that is breaking my code is there:

//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 class is in 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
};

The error I'm getting from compiler:

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 ==========

I looked for circular dependencies with #include but:

  1. I use #pragma once in every header
  2. I found that I include interface.h in allegroHelper.h and allegroHelper.h in interface.h and it was compiling without problem for long time.

Can you tell me what am I doing wrong? Or any hints what to check?

ANSWER

Ok. I dont have much time and because I didn't find any way to have pointer to Robot class in Interface, I'm accessing data by references to some data in Robot class.

In main file:

//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);
(...)

In Interface class:

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

In Robot class:

//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;
    (...)

Upvotes: 0

Views: 3450

Answers (1)

moswald
moswald

Reputation: 11677

I had to dig into your project to see the problem. You are including interface.h in robot.h through allegroHelper.h, and therefore Robot is not defined the first time you see its use.

Instead of using #include, you can forward-declare a lot of what you're doing.

#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 {...};

Upvotes: 2

Related Questions