This is making me think I'm not using the pointer correctly.
main.cpp
#include <iostream>
#include "room.h"
void initializeRooms(std::vector<Room*>& rooms);
int main() {
std::vector<Room*> rooms;
initializeRooms(rooms);
Room* r = rooms[0];
std::cout << "You are in room " << r->getName() << "\n";
return 0;
}
void initializeRooms(std::vector<Room*>& rooms) {
Room roomOne {"roomOne"};
Room roomTwo {"roomTwo"};
Exit exit { &roomOne, &roomTwo };
roomOne.addExit(&exit);
rooms.push_back(&roomOne);
rooms.push_back(&roomTwo);
}
exit.cpp
class Room;
struct Exit {
Room* room_one;
Room* room_two;
};
room.h
#ifndef ROOM_H
#define ROOM_H
#include <string>
#include <vector>
#include "exit.cpp"
class Room {
private:
std::string name;
std::vector<Exit*> exits;
public:
Room(std::string n): name{n} {
}
void addExit(Exit* e);
std::string& getName();
};
#endif
room.cpp
#include "room.h"
void Room::addExit(Exit* e) {
exits.push_back(e);
}
std::string& Room::getName() {
return name;
}
So in the main file, when the cout is called, I just see a constant loop of empty lines being output when i run the compiled file. Just keeping it simple now and using a makefile with clang
all: build
build: main.o exit.o room.o
clang++ -std=c++11 main.o exit.o room.o -o simplegame
main.o: main.cpp
clang++ -std=c++11 -c main.cpp
exit.o: exit.cpp
clang++ -std=c++11 -c exit.cpp
room.o: room.cpp
clang++ -std=c++11 -c room.cpp
clean:
rm -rf *o simplegame