What am I missing here?

Started by
2 comments, last by Plethora 10 years, 11 months ago
I've been staring a single line of code for way too long and I am very confused, maybe someone here can help me. Here's the code in question:

class Turn_Manager
{
private:
	std::vector<std::shared_ptr<iUnit>> combatUnits;

public:	
        Turn_Manager();	
 
        void init(std::vector<std::shared_ptr<iUnit>> activeUnits);
};

Turn_Manager::Turn_Manager()
{
}

void Turn_Manager::init(std::vector<std::shared_ptr<iUnit>> activeUnits)
{
	combatUnits = activeUnits;	
        //std::vector<std::shared_ptr<iUnit>> test = activeUnits;
}

The error occurs when init is called. Here is the function from which it is called:


void Battle_Map::fillUnits()
{
	std::vector<std::shared_ptr<Character>> chars = player->getChars();
	units.insert(units.end(), chars.begin(), chars.end());
	units.insert(units.end(), enemies.begin(), enemies.end());
	turnManager->init(units);
}

When I run my program, I'm getting an access violation on combatUnits. The debugger shows that within the scope of the init method, activeUnits is fine. In contains correct and accurate data. combatUnits on the other hand shows up as unreadable, and that makes no sense to me. Furthermore, when I run the program with the commented out line above instead of the combatUnits line, everything works correctly and test shows accurate and correct data (though, obviously, in goes out of scope immediately afterwards and is thus useless).

I'm just very confused as the why declaring a vector locally rather than as a class member would make any difference in this context...

Furthermore, I was just starting to implement the Turn_Manager class, what is posted above is currently the complete contents of the class. I created it in order to isolate certain functionality out of the Battle_Map class (which is too big and needs to have its responsibilities take down a few pegs). The functionality in question worked as intended when it was contained within Battle_Map.

I'm working on a game! It's called "Spellbook Tactics". I'd love it if you checked it out, offered some feedback, etc. I am very excited about my progress thus far and confident about future progress as well!

http://infinityelephant.wordpress.com

Advertisement
Are you sure that you're calling init() on a valid object?

As a side note, you're passing std::vector objects around by value which can be rather inefficient; you may want to switch to pass by reference instead.

Are you sure that you're calling init() on a valid object?

As a side note, you're passing std::vector objects around by value which can be rather inefficient; you may want to switch to pass by reference instead.

I think I'm not calling it on a valid object, I had just started to come to that conclusion by setting a breakpoint in the Turn_Manager constructor to see what values were present in there, however, the constructor doesn't appear to be getting called. However, I'm not completely sure why that is. I'm somewhat new to extensive use of smart pointers, but in order to initialize I followed what I found here:

http://msdn.microsoft.com/en-us/library/vstudio/hh279676.aspx

My declaration and initialization of turnManager looks like this:


class Battle_Map : public iHasDrawables
{
private:
    std::unique_ptr<Turn_Manager> turnManager;

public:
    Battle_Map();

private:
    fillUnits();
};

Battle_Map::Battle_Map()
    :   turnManager(std::unique_ptr<Turn_Manager>(new Turn_Manager()))
{
    ...
}

Should this not result in a proper implementation?

(Thanks for the tip on the pass by reference)

I'm working on a game! It's called "Spellbook Tactics". I'd love it if you checked it out, offered some feedback, etc. I am very excited about my progress thus far and confident about future progress as well!

http://infinityelephant.wordpress.com

I found the problem! I knew it must've been my idiocy! lol

I have an alternate constructor for Battle_Map. It was deprecated and it passed in a variable I wasn't even using anymore. I didn't realize my program was still calling that old constructor!

Let this be a lesson to anyone that runs across this in the future, REMOVE OLD CODE! :)

Anyway, thank you for the help nonetheless!

I'm working on a game! It's called "Spellbook Tactics". I'd love it if you checked it out, offered some feedback, etc. I am very excited about my progress thus far and confident about future progress as well!

http://infinityelephant.wordpress.com

This topic is closed to new replies.

Advertisement