Declare local classes as public

1

Hello, I have a problem that explodes my head :

I'm doing a script in C ++ with the purpose of making a game. All was well when I ended up in a case that, from what I researched, I found no solution in ST. 1st the script, then the problem itself:

#include "stdafx.h" 
#include "finish.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <time.h>
#include <random>
#include "enemy.h"
#include "player.h"

using namespace std;

int main() {

finish d;

cout << "Welcome to the most epic game ever!" << "\n\n";

string action;

int rnd;
rnd = d.random_calc(1,2);

switch(rnd){

case 1:

    Monster *enm;
    enm->Encounter(enm->type);
    break;

case 2:

    Ninja *enm;
    enm->Encounter(enm->type);
    break;

}

cout << "Actual life:" /*<<*/   << endl;

Player p(10, 15, enm());
p.actionPrompt(&action, true);

cout << action; 

d.end();

}

Not to take up too much space, follow the Github repository link for this project (With all classes, I recommend seeing them to make it easier for me to respond)

The problem, after all. I have to choose, in the program, between creating one of two classes: "Monster" and "Ninja", both derived from the class "Enemy". Now, I get compilation errors for the created classes, because they are created as local (within the switch) and can not be accessed outside of it.

Please, if you know, Give me a solution!

    
asked by anonymous 14.11.2016 / 01:55

2 answers

2
___ erkimt ___ Declare local classes as public ______ qstntxt ___

Hello, I have a problem that explodes my head :

I'm doing a script in C ++ with the purpose of making a game. All was well when I ended up in a case that, from what I researched, I found no solution in ST. 1st the script, then the problem itself:

enm = new Monster();

...

enm = new Enemy();

Not to take up too much space, follow the Github repository link for this project (With all classes, I recommend seeing them to make it easier for me to respond)

The problem, after all. I have to choose, in the program, between creating one of two classes: "Monster" and "Ninja", both derived from the class "Enemy". Now, I get compilation errors for the created classes, because they are created as local (within the switch) and can not be accessed outside of it.

Please, if you know, Give me a solution!

    
______ ___ azszpr165327

You can set a pointer "generic" to the parent class Enemy just before the switch: Enemy *enm , and within the switch make the appropriate instantiations:

enm = new Monster();

...

enm = new Enemy();

Note that this was missing anyway in the original code; you can not just define a pointer and use it, must first pass a valid address object through new .

    
______ azszpr165336 ___

Just declare a variable of type Enemy BEFORE the switch and in the corresponding blocks you just start the variable, after all, it will already be declared .

    
___
14.11.2016 / 02:12
1

Just declare a variable of type Enemy BEFORE the switch and in the corresponding blocks you just start the variable, after all, it will already be declared .

    
14.11.2016 / 06:44