I'm writing C++ code in order to practice and learn for myself only. But I am having trouble instantiating one of my custom objects.
I'll try to be brief in my explanation here (but also trying not to miss necessary details): Game is Text-Only. Dungeon class is the class I have trouble creating instance of. I also have a 'Game' class (when the Game class is instantiated, the results are as I expected).
In my Dungeon constructor I put a cout
with some log info. But it never prints. However the initialization code within the same code block all appears to have happened. (I know this because if I call a function to print the value of one of its variables I can see that it has indeed been populated with the data).
(The line inside the Game() constructor which calls the Debug_PrintRaw() function, this gives the desired result which implies to me that the constructor Dungeon(string filename) is called, because the string is converted from the file at that moment and it has worked the full string (the contents of the file) are printed. But none of the LOG parts are printed. (I expected them to get printed as soon as the game starts, since at the moment Game is spawned, it should spawn a Dungeon also.
So the total project code isn't too large, its just started so I will post it all here (the code is probably very stupid and overblown because I don't always know what I am doing!):
(thanks for any help)
//MAIN.CPP
#include "Game.h"
Game game = Game();
bool quit_game = 0;
int main()
{
while (!quit_game)
{
}
return 0;
}
//GAME.H
#pragma once
#include "Dungeon.h"
#include "Player.h"
class Game
{
public:
Game();
Dungeon currentDungeon = Dungeon("Dungeons/dungeon1.txt");
void ClearScreen();
void ShowSplashScreen();
};
//GAME.CPP
#include "Game.h"
#include <iostream>
Game::Game()
{
ShowSplashScreen();
currentDungeon.Debug_PrintRaw(); //<- with this line included, we do get the desired string printed
}
void Game::ClearScreen()
{
std::cout << "" << std::endl;
system("cls");
}
void Game::ShowSplashScreen()
{
ClearScreen();
std::cout << " :: ::::: ::" << std::endl;
std::cout << " :: ::::: ::" << std::endl;
std::cout << " :: ::::: ::" << std::endl;
std::cout << " :: ::::: ::" << std::endl << std::endl << std::endl;
std::cout << " .:: ::::: ::." << std::endl;
std::cout << " .::: ::::: :::." << std::endl;
std::cout << " .:::' ::::: ':::." << std::endl;
std::cout << " .::::' ::::: '::::." << std::endl;
std::cout << " .::::' ::::: '::::." << std::endl;
std::cout << " .:::::' ::::: ':::::." << std::endl;
std::cout << " .::::::' ::::: '::::::." << std::endl;
std::cout << " ...:::::::' ::::: ':::::::..." << std::endl;
std::cout << " :::::::'' ::::: '':::::::" << std::endl;
std::cout << " ::::'' ::::: ''::::" << std::endl;
std::cout << "~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~" << std::endl;
std::cout << " fAtari Games " << std::endl;
std::cout << "~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~" << std::endl;
}
//DUNGEON.H
#pragma once
#include <vector>
#include "RoomData.h"
class Dungeon
{
public:
Dungeon(std::string filename);
std::string rawLevelText;
std::vector<RoomData> rooms;
void Debug_PrintRaw();
};
//DUNGEON.CPP
#include "Dungeon.h"
#include <fstream>
#include <sstream>
#include <iostream>
Dungeon::Dungeon(std::string filename)
{
std::cout << "LOG: Dungeon Constructor" << std::endl;
std::ifstream file(filename);
std::stringstream buffer;
buffer << file.rdbuf();
rawLevelText = buffer.str();
Debug_PrintRaw();
}
void Dungeon::Debug_PrintRaw()
{
std::cout << "LOG: Dungeon PrintRaw Called." << std::endl;
std::cout << rawLevelText << std::endl;
}