Public Group

# What am I missing here?

This topic is 2025 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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. Edited by Plethora

##### Share on other sites
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.

##### Share on other sites

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)

##### Share on other sites

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!

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633709
• Total Posts
3013481
×