0

I'm making a program with a global vector filled with objects and putting it in a namespace here's the code:

Character.h:

#include <vector>
class Character
{
  //Constructor and variable declaration, etc.
}
class Player: public Character
{
  //Constructor and variable declaration, etc.
}
namespace chr
{
 extern std::vector<Character> characters;
 extern Player player_one;
}

Character.cpp:

namespace chr
{
 Player player_one("name");
 std::vector <Character> characters;
}

main:

#include <SFML/Graphics.hpp> //I'm using the SFML libraries
#include "Personaje.h"

 int main()
 {
 Player player_two("name"); //just to test that single object work (they do)
 chr::personajes.push_back(chr::player_one); //HERE'S THE ISSUE
 
  //sample SFML code to check if the rest of the program works
 sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
 sf::CircleShape shape(100.f);
 shape.setFillColor(sf::Color::Green);

 while (window.isOpen())
 {
    sf::Event event;
    while (window.pollEvent(event))
    {
        if (event.type == sf::Event::Closed)
            window.close();

    }

    window.clear();
    window.draw(shape);
    window.display();
 }

return 0;
}

When I build this code (I'm using code::Blocks) it actually compiles without errors, but when I run it, as soon as the console comes up (the SFML window doesn't even pop up) it freezes and a Windows notification appears informing me that my program has stopped working. I know it's the code on the chr namespace that is causing this because if I comment it out the program runs fine, but I don't know why this is. If it's of any help, when the program crashes Windows opens the Visual Studio debugger, which produces this message:

Exception thrown at 0x0044EFA2 in wtfhappened.exe: 0xC0000005: Access violation reading location 0xFFFFFFFC.

If there is a handler for this exception, the program may be safely continued.

Thanks for your time

Community
  • 1
  • 1
Daniel P.
  • 9
  • 2
  • What debuggers are made for. If you think `//Constructor and variable declaration, etc.` for both `Character` and `Player` have nothing to do with this, I beg to differ until we *see that code*. My wild-ass guess is you're violating the [Rule of Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) in one, or both, of those classes. Or an [object slicing](http://stackoverflow.com/questions/274626/what-is-object-slicing) problem. Or perhaps both. And btw, `std::vector characters;` doesn't match your `std::vector personajes;`. – WhozCraig Jul 31 '16 at 19:40
  • Code::Blocks is not a compiler but an IDE/Editor ... – Torbjörn Jul 31 '16 at 20:20
  • What? Windows automatically opens the VS debugger? All by itself? Come again? –  Jul 31 '16 at 20:59

1 Answers1

0
std::vector <Character> characters;

This is a container for Characters. Not Players.

chr::personajes.push_back(chr::player_one);

This will take your Player, and create a new Character from it to put into the vector. All your information that Player has on top of Character will be lost and you are prone to something called slicing. I'll take a guess and say this is not what you want.

There are multiple ways to handle this. Start with not having a container of Characters, but a container of Character references (used in the plain English sense here, not C++ references). You may want to look into smart pointers (i.e. std::shared_ptr<>) for this to be as painless as possible.

Community
  • 1
  • 1
nvoigt
  • 75,013
  • 26
  • 93
  • 142