c++,很多错误.也许是循环依赖

C++ , Lots of Errors. Maybe Circular Dependency?

本文关键字:循环 依赖 也许 错误 c++      更新时间:2023-10-16

我正在制作一款游戏,它有一堆类链接在一起。本来很好,但现在一团糟。

1>------ Build started: Project: Razoo, Configuration: Debug Win32 ------
1>  Main.cpp
1>c:usersronandesktopc++razoorazooinventory.h(62): warning C4172: returning address of local variable or temporary
1>c:usersronandesktopc++razoorazooinventory.h(65): warning C4172: returning address of local variable or temporary
1>c:usersronandesktopc++razoorazooplayer.h(21): error C2143: syntax error : missing ';' before '*'
1>c:usersronandesktopc++razoorazooplayer.h(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:usersronandesktopc++razoorazooplayer.h(24): error C2061: syntax error : identifier 'Room'
1>c:usersronandesktopc++razoorazooplayer.h(76): error C2143: syntax error : missing ';' before '*'
1>c:usersronandesktopc++razoorazooplayer.h(76): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:usersronandesktopc++razoorazooplayer.h(77): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:usersronandesktopc++razoorazooplayer.h(79): warning C4183: 'GetCurrentRoom': missing return type; assumed to be a member function returning 'int'
1>c:usersronandesktopc++razoorazooplayer.h(31): error C2065: 'm_currentRoom' : undeclared identifier
1>c:usersronandesktopc++razoorazooplayer.h(31): error C2065: 'p_currentRoom' : undeclared identifier
1>c:usersronandesktopc++razoorazooplayer.h(32): error C2065: 'p_inventory' : undeclared identifier
1>c:usersronandesktopc++razoorazooplayer.h(42): error C2065: 'm_currentRoom' : undeclared identifier
1>c:usersronandesktopc++razoorazooplayer.h(78): error C2065: 'm_currentRoom' : undeclared identifier
1>c:usersronandesktopc++razoorazooplayer.h(108): error C2065: 'm_currentRoom' : undeclared identifier
1>c:usersronandesktopc++razoorazooplayer.h(108): error C2227: left of '->GetNorth' must point to class/struct/union/generic type
1>          type is ''unknown-type''
1>c:usersronandesktopc++razoorazooplayer.h(113): error C2065: 'm_currentRoom' : undeclared identifier
1>c:usersronandesktopc++razoorazooplayer.h(113): error C2227: left of '->GetSouth' must point to class/struct/union/generic type
1>          type is ''unknown-type''
1>c:usersronandesktopc++razoorazooplayer.h(118): error C2065: 'm_currentRoom' : undeclared identifier
1>c:usersronandesktopc++razoorazooplayer.h(118): error C2227: left of '->GetEast' must point to class/struct/union/generic type
1>          type is ''unknown-type''
1>c:usersronandesktopc++razoorazooplayer.h(123): error C2065: 'm_currentRoom' : undeclared identifier
1>c:usersronandesktopc++razoorazooplayer.h(123): error C2227: left of '->GetWest' must point to class/struct/union/generic type
1>          type is ''unknown-type''
1>c:usersronandesktopc++razoorazooplayer.h(156): error C2065: 'm_currentRoom' : undeclared identifier
1>c:usersronandesktopc++razoorazooplayer.h(158): error C2065: 'm_currentRoom' : undeclared identifier
1>c:usersronandesktopc++razoorazooplayer.h(158): error C2227: left of '->GetName' must point to class/struct/union/generic type
1>          type is ''unknown-type''
1>c:usersronandesktopc++razoorazoogeneralutils.h(22): error C2227: left of '->GetName' must point to class/struct/union/generic type
1>          type is 'int *'
1>c:usersronandesktopc++razoorazoogeneralutils.h(22): error C2228: left of '.length' must have class/struct/union
1>c:usersronandesktopc++razoorazoogeneralutils.h(27): error C2227: left of '->GetName' must point to class/struct/union/generic type
1>          type is 'int *'
1>c:usersronandesktopc++razoorazoogeneralutils.h(72): error C2227: left of '->GetDesc' must point to class/struct/union/generic type
1>          type is 'int *'
1>c:usersronandesktopc++razoorazoogeneralutils.h(84): error C2227: left of '->GetNorth' must point to class/struct/union/generic type
1>          type is 'int *'
1>c:usersronandesktopc++razoorazoogeneralutils.h(88): error C2227: left of '->GetNorth' must point to class/struct/union/generic type
1>          type is 'int *'
1>c:usersronandesktopc++razoorazoogeneralutils.h(88): error C2227: left of '->GetName' must point to class/struct/union/generic type
1>c:usersronandesktopc++razoorazoogeneralutils.h(91): error C2227: left of '->GetSouth' must point to class/struct/union/generic type
1>          type is 'int *'
1>c:usersronandesktopc++razoorazoogeneralutils.h(95): error C2227: left of '->GetSouth' must point to class/struct/union/generic type
1>          type is 'int *'
1>c:usersronandesktopc++razoorazoogeneralutils.h(95): error C2227: left of '->GetName' must point to class/struct/union/generic type
1>c:usersronandesktopc++razoorazoogeneralutils.h(98): error C2227: left of '->GetEast' must point to class/struct/union/generic type
1>          type is 'int *'
1>c:usersronandesktopc++razoorazoogeneralutils.h(102): error C2227: left of '->GetEast' must point to class/struct/union/generic type
1>          type is 'int *'
1>c:usersronandesktopc++razoorazoogeneralutils.h(102): error C2227: left of '->GetName' must point to class/struct/union/generic type
1>c:usersronandesktopc++razoorazoogeneralutils.h(105): error C2227: left of '->GetWest' must point to class/struct/union/generic type
1>          type is 'int *'
1>c:usersronandesktopc++razoorazoogeneralutils.h(109): error C2227: left of '->GetWest' must point to class/struct/union/generic type
1>          type is 'int *'
1>c:usersronandesktopc++razoorazoogeneralutils.h(109): error C2227: left of '->GetName' must point to class/struct/union/generic type
1>c:usersronandesktopc++razoorazoogeneralutils.h(115): error C2227: left of '->GetInventory' must point to class/struct/union/generic type
1>          type is 'int *'
1>c:usersronandesktopc++razoorazoogeneralutils.h(115): error C2228: left of '.Size' must have class/struct/union
1>c:usersronandesktopc++razoorazoogeneralutils.h(116): error C2227: left of '->GetInventory' must point to class/struct/union/generic type
1>          type is 'int *'
1>c:usersronandesktopc++razoorazoogeneralutils.h(116): error C2228: left of '.GetItemNames' must have class/struct/union
1>c:usersronandesktopc++razoorazoogeneralutils.h(154): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:usersronandesktopc++razoorazooinitializer.h(128): error C2143: syntax error : missing ';' before '&'
1>c:usersronandesktopc++razoorazooinitializer.h(128): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:usersronandesktopc++razoorazooinitializer.h(128): error C2061: syntax error : identifier 'HashTable'
1>c:usersronandesktopc++razoorazooinitializer.h(129): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:usersronandesktopc++razoorazooinitializer.h(214): warning C4183: 'InitializeRooms': missing return type; assumed to be a member function returning 'int'
1>c:usersronandesktopc++razoorazooinitializer.h(216): error C2061: syntax error : identifier 'HashTable'
1>c:usersronandesktopc++razoorazooinitializer.h(130): error C2065: 'p_filename' : undeclared identifier
1>c:usersronandesktopc++razoorazooinitializer.h(155): error C2065: 'Room' : undeclared identifier
1>c:usersronandesktopc++razoorazooinitializer.h(155): error C2065: 'temp' : undeclared identifier
1>c:usersronandesktopc++razoorazooinitializer.h(183): error C2065: 'Room' : undeclared identifier
1>c:usersronandesktopc++razoorazooinitializer.h(183): error C2146: syntax error : missing ';' before identifier 'room'
1>c:usersronandesktopc++razoorazooinitializer.h(183): error C2065: 'temp' : undeclared identifier
1>c:usersronandesktopc++razoorazooinitializer.h(183): error C3861: 'room': identifier not found
1>c:usersronandesktopc++razoorazooinitializer.h(185): error C2065: 'p_roomtable' : undeclared identifier
1>c:usersronandesktopc++razoorazooinitializer.h(185): error C2228: left of '.Insert' must have class/struct/union
1>          type is ''unknown-type''
1>c:usersronandesktopc++razoorazooinitializer.h(185): error C2065: 'room' : undeclared identifier
1>c:usersronandesktopc++razoorazooinitializer.h(185): error C2228: left of '.GetName' must have class/struct/union
1>          type is ''unknown-type''
1>c:usersronandesktopc++razoorazooinitializer.h(202): error C2065: 'p_roomtable' : undeclared identifier
1>c:usersronandesktopc++razoorazooinitializer.h(202): error C2228: left of '.Find' must have class/struct/union
1>          type is ''unknown-type''
1>c:usersronandesktopc++razoorazooinitializer.h(202): error C2227: left of '->m_data' must point to class/struct/union/generic type
1>c:usersronandesktopc++razoorazooinitializer.h(202): error C2228: left of '.SetNorth' must have class/struct/union
1>c:usersronandesktopc++razoorazooinitializer.h(204): error C2065: 'p_roomtable' : undeclared identifier
1>c:usersronandesktopc++razoorazooinitializer.h(204): error C2228: left of '.Find' must have class/struct/union
1>          type is ''unknown-type''
1>c:usersronandesktopc++razoorazooinitializer.h(204): error C2227: left of '->m_data' must point to class/struct/union/generic type
1>c:usersronandesktopc++razoorazooinitializer.h(204): error C2228: left of '.SetSouth' must have class/struct/union
1>c:usersronandesktopc++razoorazooinitializer.h(206): error C2065: 'p_roomtable' : undeclared identifier
1>c:usersronandesktopc++razoorazooinitializer.h(206): error C2228: left of '.Find' must have class/struct/union
1>          type is ''unknown-type''
1>c:usersronandesktopc++razoorazooinitializer.h(206): error C2227: left of '->m_data' must point to class/struct/union/generic type
1>c:usersronandesktopc++razoorazooinitializer.h(206): error C2228: left of '.SetEast' must have class/struct/union
1>c:usersronandesktopc++razoorazooinitializer.h(208): error C2065: 'p_roomtable' : undeclared identifier
1>c:usersronandesktopc++razoorazooinitializer.h(208): error C2228: left of '.Find' must have class/struct/union
1>          type is ''unknown-type''
1>c:usersronandesktopc++razoorazooinitializer.h(208): error C2227: left of '->m_data' must point to class/struct/union/generic type
1>c:usersronandesktopc++razoorazooinitializer.h(208): error C2228: left of '.SetWest' must have class/struct/union
1>c:usersronandesktopc++razoorazooinitializer.h(212): error C2146: syntax error : missing ';' before identifier 'level'
1>c:usersronandesktopc++razoorazooinitializer.h(212): error C2065: 'p_roomtable' : undeclared identifier
1>c:usersronandesktopc++razoorazooinitializer.h(212): error C2228: left of '.Find' must have class/struct/union
1>          type is ''unknown-type''
1>c:usersronandesktopc++razoorazooinitializer.h(212): error C2227: left of '->m_data' must point to class/struct/union/generic type
1>c:usersronandesktopc++razoorazooinitializer.h(212): error C3861: 'level': identifier not found
1>c:usersronandesktopc++razoorazooinitializer.h(213): error C2065: 'level' : undeclared identifier
1>c:usersronandesktopc++razoorazooinitializer.h(218): error C2065: 'p_filename' : undeclared identifier
1>c:usersronandesktopc++razoorazooinitializer.h(231): error C2065: 'Room' : undeclared identifier
1>c:usersronandesktopc++razoorazooinitializer.h(231): error C2065: 'currentRoom' : undeclared identifier
1>c:usersronandesktopc++razoorazooinitializer.h(253): error C2065: 'currentRoom' : undeclared identifier
1>c:usersronandesktopc++razoorazooinitializer.h(253): error C2065: 'p_rooms' : undeclared identifier
1>c:usersronandesktopc++razoorazooinitializer.h(253): error C2228: left of '.Find' must have class/struct/union
1>          type is ''unknown-type''
1>c:usersronandesktopc++razoorazooinitializer.h(253): error C2227: left of '->m_data' must point to class/struct/union/generic type
1>c:usersronandesktopc++razoorazooinitializer.h(263): error C2065: 'p_numItems' : undeclared identifier
1>c:usersronandesktopc++razoorazooinitializer.h(265): error C2065: 'p_items' : undeclared identifier
1>c:usersronandesktopc++razoorazooinitializer.h(265): error C2228: left of '.GetName' must have class/struct/union
1>c:usersronandesktopc++razoorazooinitializer.h(267): error C2065: 'p_items' : undeclared identifier
1>c:usersronandesktopc++razoorazooinitializer.h(272): error C2065: 'currentRoom' : undeclared identifier
1>d:programsmicrosoft visual studio 11.0vcincluderegex(1670): fatal error C1903: unable to recover from previous error(s); stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我想我有一个不匹配的大括号,或者缺少一个;但是我确实检查了代码中的每一个错误,所以我认为我已经排除了这个问题。

从第一个错误开始:

1>c:usersronandesktopc++razoorazooplayer.h(21): error C2143: syntax error : missing ';' before '*'

和它指向的代码:

    #ifndef PLAYER_H
#define PLAYER_H
#include <string>
#include <fstream>
#include "Room.h"
#include "Inventory.h"
#include "Array.h"

class Player
{
private:
    string m_name;
    int m_health;
    int m_confidence;
    int m_humor;
    int m_speed;
    Room* m_currentRoom; //Line 21
    Inventory m_inventory;

在我看来,#include "Room.h"不能正常工作?
我是否混淆了某种循环依赖关系(我现在甚至不完全理解它们)?
或者是否有其他一些"常见"问题会同时引发所有这些错误?
在这里的第一个帖子,所以不确定你是否需要一些更多的代码或任何东西(或者如果我搞砸了任何语法!)
谢谢!

您可以通过从"Player.h"中删除#include "Room.h"并将该行替换为以下内容来解决循环依赖:

class Room;

这将工作得很好,因为它看起来你只使用一个指向Room类的指针。在player。cpp中,如果你需要Room的方法和成员或者你需要解引用指针,你可以在顶部这样做:

#include "Player.h"
#include "Room.h"

如果这不起作用,你能发布更多的代码吗?