0

For context, I'm trying to recreate a basic sort of text based adventure game I made in high school in C++. Essentially the code creates different "rooms" as objects and links them together. I'm having an issue in C++ with the function which links two rooms together in a certain direction, "link()". Error: "undefined reference to `Rooms::east'". The "visit()" function can then be used to change between linked rooms. How can I change my C++ code in order to fix the issue and still work in a similar fashion to my Java code?

Additionally, my university is requiring us to use a library which works with one of their simulators(FEHLCD.h), but I do not believe this is what is causing the issue.

Java code:

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++ rooms.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++ rooms.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
}

1 Answers1

0

As mentioned in this question, you simply need to make sure to provide a definition for Rooms::east in your implementation file:

bool Rooms::southcheck = false;
Rooms Rooms::east;
OLEGSHA
  • 388
  • 3
  • 13