-1

I've been trying to pass my Graphics Manager class to both my Robot and Room class.

But when attempting to pass the class by reference I get 3 errors regarding the pass by reference.

These are the errors I'm referring to: C2143 syntax error: missing ';' before '*'

C4430 missing type specifier - int assumed. Note: C++ does not support default-int

C2238 unexpected token(s) preceding ';'

I have attempted to change the way I've been passing the classes but with no luck, I have highlighted the areas in which is causing the error as well as the code that i have tried to use to fix the problem.

Any advice in how i could go about fixing these errors is highly appreciated.

I have not included the full .cpp files as they are quite large but I will include a link to a pasteBin with the full script.

GrapicsManager.h

#pragma once
#include <iostream>
#include <SFML/Graphics.hpp>
#include "Room.h"
#include "Robot.h"


class GraphicsManager
{
 public:


Room* room;     //This does not Flag Up Errors 
Robot* robot;   //This does not Flag Up Errors 

Robot.h

#pragma once
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/System/String.hpp>
#include "GraphicsManager.h"
//#include "Room.h" //This what i had
class Room; //This is what i changed
//class GraphicsManager; //Wasnt sure if i should use it this 
//way

class Robot
{
public:

//Graphics Variables
Room* room;     //This works after the change
Robot* robot;   //This works after the change

GraphicsManager *gm;    //This throughs up the error

//This Is what i attemped to use with no effect
//GraphicsManager* gm = new GraphicsManager(room, robot);

Robot.cpp https://pastebin.com/Xd1A3Vii

#include "Robot.h"




Robot::Robot()
{

gm = new GraphicsManager(room, robot); //This tells me gm is 
//not declared
this->room = room; //This does not flag up errors
this->robot = robot; //This does not flag up errors

//Room &room = *rm;  // attempted to use this but decided not 
//to

}

Room.h

#pragma once
#include <SFML/Graphics.hpp>
#include <SFML/System/String.hpp>
#include "GraphicsManager.h" //
//#include "Robot.h"    //what i orginally had
//class GraphicsManager;    //i decided not to do it this way
class Robot;    //What i changed it to 


class Room
{
public:

//Reference to other classes
Room* room;     //This doesnt throw errors
Robot* robot;   //This doesnt throw errors

//Refference to graphics manager
GraphicsManager *gm; //This throws the three errors mentioned
};

Room.cpp https://pastebin.com/6R6vnVfy

#include "Room.h"



Room::Room()
{

gm = new GraphicsManager(room, robot);
this->room = room;
this->robot = robot;
  • They all seem like trivial typos. Perhaps just include the lines errors are on and 3 or 4 lines either side (often these type of errors could come from a mistake on a line a few lines before the one reported) – John3136 Nov 28 '18 at 21:27
  • https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes – drescherjm Nov 28 '18 at 21:50

1 Answers1

0

It's the classic cicular include issue. GrapicsManager.h includes Room.h and Robot.h which each include GrapicsManager.h again. Now, for example, when compiling GraphicsManager.cpp you include GrapicsManager.h. But before you ever get to the GraphicsManager class definition, you first include Room.h. From there you go straight to include GrapicsManager.h again, but since you have a #pragma once in there, the compiler will simply skip that include. By the time the compiler then gets to the GraphicsManager *gm; member declaration in Room.h, is has never seen a declaration of a type named GraphicsManager. The error message that Visual C++ gives you then

C4430 missing type specifier - int assumed. Note: C++ does not support default-int

is arguably a bit unintuitive. At the point where it encounters the identifier GraphicsManager, an identifier can only mean the start of a declaration. Since GraphicsManager is not a known type, the compiler assumes that identifier must be the name of the entity that is supposed to be declared and you just forgot to specify the type. That's why you get the error message you see. C in the olden days used to allow you to omit the type specifier in a declaration which would just mean to use int as a default. So you would see this error as a result of trying to compile ancient, non-standard C code. That's why the error message contains the explicit note that that's not allowed…

You already added forward declarations for Room in Robot.h and for Robot in Room.h. You'll have to do the same for GraphicsManager

Michael Kenzel
  • 15,508
  • 2
  • 30
  • 39