【发布时间】:2018-11-28 21:24:48
【问题描述】:
我一直在尝试将我的 Graphics Manager 类传递给我的 Robot 和 Room 类。
但是当试图通过引用传递类时,我得到了 3 个关于通过引用传递的错误。
这些是我所指的错误: C2143 语法错误:缺少 ';'在'*'之前
C4430 缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-int
C2238 ';' 之前的意外标记
我试图改变我通过课程的方式,但没有运气,我突出显示了导致错误的区域以及我试图用来解决问题的代码。
非常感谢任何关于如何解决这些错误的建议。
我没有包含完整的 .cpp 文件,因为它们非常大,但我将包含一个指向带有完整脚本的 pasteBin 的链接。
GraphicsManager.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
}
房间.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;
【问题讨论】:
-
它们看起来都是些微不足道的错别字。也许只包括错误所在的行和两边的 3 或 4 行(通常这些类型的错误可能来自报告前几行的错误)
标签: c++ visual-studio class pointers reference