首页 > 解决方案 > 我正在尝试重新创建我在 C++ 中创建的 Java 代码,但不断收到此错误:“未定义对 'Rooms::east' 的引用”

问题描述

对于上下文,我正在尝试重新创建我在高中时用 C++ 制作的一种基本的基于文​​本的冒险游戏。本质上,代码创建了不同的“房间”作为对象并将它们链接在一起。我在 C++ 中遇到了一个问题,该函数将两个房间在某个方向“link()”链接在一起。错误:“未定义对‘Rooms::east’的引用”。然后可以使用“visit()”函数在链接的房间之间进行切换。如何更改我的 C++ 代码以解决问题并仍然以与我的 Java 代码类似的方式工作?

此外,我的大学要求我们使用与他们的模拟器之一(FEHLCD.h)一起使用的库,但我不认为这是导致问题的原因。

Java代码:

import java.util.*;

public class Room
{
    
    private Room left; 
    private Room right;
    private Room forwards;
    private Room backwards;
    private String description;
    private boolean hasMagicSkeletonKey=false;
    private static boolean characterHasMagicSkeletonKey=false;

    final static int LEFT=0;
    final static int RIGHT=1;
    final static int FORWARDS=2;
    final static int BACKWARDS=3;
    
    public Room(String description)
    {
        this.description=description;
        
    }

    public void link(Room room,int direction)
    {
        if(direction==LEFT)left=room;
        else if(direction==RIGHT)right=room;           //This is the part of the code in the java version I am currently trying to replicate.
        else if(direction==FORWARDS)forwards=room;
        else if(direction==BACKWARDS)backwards=room;
        else
        {
            throw new IllegalArgumentException("You run straight into a wall, and the pain of the impact makes you question why you did it.\n");
        }       
    }

    public void visit()
    {
        System.out.println(description);
        if(left==null && right==null && forwards==null && backwards==null)
        {
            if(characterHasMagicSkeletonKey)
            {
                System.out.println("You walk up to the locked door and put the key in the lock. The room is dark, but you faintly make out the shape of a desk with a book sitting on it. The title of the book, from what you can make out, is:\"How to pass PS&E with an A: A comprehnsive guide for Juniors.\" You excitedly sit down and try to read it, but due to it being 50,000 pages long, you figure that you'll be there for a while.");
                return;
            }
            else
            {
                System.out.println("You knock on the door, but there isn't an answer. You then try to jiggle the door handle, but the door doesn't budge because it's locked. You keel over from a mixture of exhaustion, laziness, and the fact that you no longer feel like continuing with school. Game Over.");
                
                return;
            }
        }
        if(hasMagicSkeletonKey)
        {
            System.out.println("On the table near the back of the room you see a skelton key. It is glowing red and continuously making a loud, puslating noise.\n");
        }
        
        if(left!=null)
        {
            System.out.println("There is a door to your left.");
        }
        if(right!=null)
        {
            System.out.println("There is a door to your right.");
        }
        if(forwards!=null)
        {
            System.out.println("There is a door in front of you.");
        }
        if(backwards!=null)
        {
            System.out.println("There is a door behind you");
        }
        System.out.println("\n\n");
        
        System.out.print("What do you want to do? ");
        Scanner scan=new Scanner(System.in);
        String prompt=scan.nextLine();
        prompt=prompt.toLowerCase().trim();
        if(prompt.contains("left") && left!=null)
        {
            left.visit();
            return;
        }
        else if(prompt.contains("right") && right!=null)
        {
            right.visit();
            return;
        }
        else if(prompt.contains("forward") && forwards!=null)
        {
            forwards.visit();
            return;
        }
        else if(prompt.contains("backward") && backwards!=null)
        {
            backwards.visit();
            return;
        }
        else if(prompt.contains("grab") && hasMagicSkeletonKey)
        {
            System.out.println("The key suddenly stopped making noise, and it's strangely calming to hold.\n");
            hasMagicSkeletonKey=false;
            characterHasMagicSkeletonKey=true;
            this.visit();
            return;
        }
        else if(prompt.contains("inventory"))
        {
            if(characterHasMagicSkeletonKey)
            {
                System.out.println("You have a red glowing, noisy skeleton key.\n");
            }
            else
            {
                System.out.println("You don't have anything.\n");
            }
            this.visit();
            return;
        }
        else
        {
            this.visit();
            return;
        }
    }
    
    public void placeMagicSkeletonKey()
    {
        hasMagicSkeletonKey=true;
    }
}

C++ 房间.h:

#ifndef ROOM_H
#define ROOM_H

#include "FEHLCD.h"
using namespace std;

class Rooms;

class Rooms {
private:
    char chardescription[256];
    static bool eastcheck, westcheck, northcheck, southcheck;

public:
    static Rooms east, west, north, south;
    Rooms();
    Rooms(char*); //constructors
    void printData(void);
    static void link(Rooms, int);
    void visit();
    static const int WEST = 0, EAST = 1, NORTH = 2, SOUTH = 3;
};

#endif

C++ 房间.cpp:

#include "rooms.h"
#include "FEHLCD.h"
#include "string.h"
using namespace std;

bool Rooms::eastcheck = false;
bool Rooms::westcheck = false;
bool Rooms::northcheck = false;
bool Rooms::southcheck = false;


Rooms::Rooms() {

    char placeholder[] = "unknown";

    strcpy(chardescription, placeholder);
}

Rooms::Rooms(char* desc) {

    strcpy(chardescription, desc);
}

void Rooms::printData(void) {
    LCD.WriteLine(chardescription);
}

//links two rooms together by direction
void Rooms::link(Rooms room, int direction) {

    /*if(direction==WEST){
        west=room;
        westcheck=true;
    }*/
    if (direction == EAST) {
        east = room;                  //this is where I get the error
        eastcheck = true;
        LCD.WriteLine("Linked");
    }
    /*else if(direction==NORTH){
        north=room;
        northcheck=true;
    }
    else if(direction==SOUTH){
        south=room;
        northcheck=
    }*/
} 

//changes the active room
void Rooms::visit(void) {
    LCD.Clear();
    LCD.WriteLine(chardescription);
    if (eastcheck == true) {
        LCD.WriteLine("There is a door to your east.");
    }

    LCD.WriteLine("What do you want to do?");
    //code incomplete
    //this is where the user would be prompted with options about which room to move to next
}

标签: javac++

解决方案


正如在这个问题中提到的,您只需要确保Rooms::east在您的实现文件中提供一个定义:

bool Rooms::southcheck = false;
Rooms Rooms::east;

推荐阅读