vector of pointers to vector objects

Apr 3, 2013 at 10:08pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
vector<MonsterType> hArmy; // MonsterType objects pushed back on hArmy using PushMonsterType (not included here)
vector<vector<MonsterType>*> Enemies; //vector of pointers to vectors of object MonsterType

for(int i=0; i<3; i++) {
    vector<MonsterType>* pAiArmy; //creates pointer to vector
    pAiArmy = new vector<MonsterType>; // new vector of objects

    for(int j=0; j<(rand()%5)+1; j++) { //adds 1-5 MonsterType objects to vector
        pushMonsterType(pAiArmy, (rand()%6)+7, (rand()%20)+1);
    }

    Enemies.push_back(pAiArmy); //pushed the adress of vector<MonsterType> on to the vector of pointers
}

//all of this works as it should - object Enemies now holds 3 adresses of vector of objects

for(int i=0; i<3; i++) {
    Combat Melee(hArmy, Enemies.at(i)); //<- argument 2 causes problems
}



combatClass.h
1
2
3
4
5
6
7
8
9
10
class Combat { 
    std::vector<MonsterType*> stackHolder;  //holds pointers to all creature stacks participating in the combat
    int hArmyStack;                         //number of creature stacks in player army
    int aiArmyStack;                        //number of creature stacks in ai army
    //...    
        
    public:
        Combat(std::vector<MonsterType>&, std::vector<MonsterType>&); //constructor
        ~Combat(); //destructor
    };


combatClass.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Combat::Combat(vector<MonsterType> &hArmy, vector<MonsterType> &aiArmy) {
    hArmyStack  = hArmy.size();
    aiArmyStack = aiArmy.size();

    //load pointer to all creatures into vector stackHolder
    {
        for(int i=0; i<hArmyStack+aiArmyStack; i++) {
            stackHolder.push_back((MonsterType*)::operator new(sizeof(MonsterType*))); //allocates memory for all pointers
        }
        for(int i=0; i<hArmyStack; i++) { //assigns pointers to all player creatures
            stackHolder.at(i) = &hArmy.at(i);
        }
        for(int i=hArmyStack, j=0; i<hArmyStack+aiArmyStack; i++,j++) {
            stackHolder.at(i) = &aiArmy.at(j);
        }
    }
    //...
}

error: no matching function for call to 'Combat::Combat(std::vector<MonsterType>&, std::vector<MonsterType>*&)'


The memory adress at Enemies.at(i) is correct, but I can't seem pass to adress the way I want. It seems the compiler treats the adress as a pointer to an adress (*& - or the adress of a pointer??)

It is rather important, that the combat class recieves the adresses of two vector<MonsterType> as arguments, but I am very open to suggestions.
Last edited on Apr 3, 2013 at 10:09pm
Apr 3, 2013 at 10:30pm
Your constructor asks for references
Your vector contains pointers

I think in your constructor you meant to use * and not &
Last edited on Apr 3, 2013 at 10:30pm
Apr 4, 2013 at 11:16am
No, I did mean to use a reference (&).

Is it not possible to pass the references pointed to in vector Enemies?
Apr 4, 2013 at 1:48pm
You seem to be confusing "reference" and "pointer". It's an easy mistake to make because to take the address of an object you use &, but & is also used for references.

In your case, you need to use pointers, not references. All you have to do is change your constructor to use * instead of &
Apr 4, 2013 at 8:51pm
Thanks. A little re-writing made it work as it should.
Topic archived. No new replies allowed.