accessing private Vectors

Started by
4 comments, last by rip-off 18 years, 6 months ago
class c_weapon
{
public:
    ~c_weapon(){
        while (firingmode.size())
        {
            delete firingmode.back();
            this->firingmode.pop_back();
        }
    }
    void initialfiringmodes(short int modes){
        firinginfo.setfiringmodes(modes);
        for(short int createmodes=0; createmodes<=modes; createmodes++){
            firingmode.push_back(new c_firingmode());
        }
    }
    c_firinginfo *getfiringinfo(){return &firinginfo}
    
    c_firingmode *getfiringmode(int index){return &firingmode[index];}//<<<<----
    
private: 
    c_firinginfo firinginfo;
    std::vector<c_firingmode*> firingmode; //<<<<---------     
};
The line //c_firingmode **getfiringmode(int index){return &firingmode[index];} is wrong. What it's supposed to do is access "firingmode" from the private declaration std::vector<c_firingmode*> firingmode; just in case you didn't realize, firingmode is an "vectored array" of instances of the class c_firingmode. Can someone who knows C++ and how Vectors work please figure out this problem. I've asked people on another forum but they've never ran into this before. Am I doing this a hard way also? If there is a better way, I would like to know. The c_firingmode is a really big class though. So to restate I need to figure out how to access a vector of pointers that are private in a class.
Advertisement
Quote:Original post by Sirisian
//...    c_firingmode *getfiringmode(int index){return &firingmode[index];}    private: //...    std::vector<c_firingmode*> firingmode;       };


The line
//c_firingmode **getfiringmode(int index){return &firingmode[index];}


im confused

which do you want
c_firingmode *getfiringmode(int index){return &firingmode[index];}
c_firingmode **getfiringmode(int index){return &firingmode[index];}

if its the first your problem is the & operator

what are you trying to do?

the operator[]( int i ) of a vector returns a reference, wouldn't that do for whatever you're doing?

[EDIT]

i just compiled the above with the ** version, it compiles

and the * version with the return statment modified to

c_firingmode *getfiringmode(int index){return firingmode[index];}

they both compiled. what isnt working?
In the most basic way. Think of it like this.

I have an array of guns that hold all the information of each gun.

c_weapon weaponlist[10];

Then I want to set how many firing modes, of say weapon 0.

weaponlist[0].initialfiringmodes(1);

That would create 1 firing mode on the gun. Then if I wanted to access the speed of the first firing mode I would say.

weaponlist[0].getfiringmode()->setspeed(20);

But, I can't create a function inside of c_weapon that will access the vector firingmodes.

EDIT

C:\Documents and Settings\Brandon Andrews\My Documents\C++\test\weapon_class.hpp In member function `c_firingmode* c_weapon::getfiringmode(int)':

164 C:\Documents and Settings\Brandon Andrews\My Documents\C++\test\weapon_class.hpp cannot convert `c_firingmode**' to `c_firingmode*' in return

I used just
c_firingmode *getfiringmode(int index){return &firingmode[index];}

I can't figure out why it says can't convert "c_firingmode**" to "c_firingmode*"

[Edited by - Sirisian on March 1, 2010 9:53:35 AM]
HEY!!! no ampersand makes it work, like you said.
You template is defined to store pointers to firing modes. Then, in the access function, you pull an element from the list and return the address of it. So you're returning a pointer to a pointer firing mode.

But the function is defined as returning a pointer to a firing mode.

Take the & out of "return &firingmode[index];"

you want to "return firingmode[index];"
yup, you were passing back the address of the element in the vector, the address of a pointer, i.e. a pointer to a pointer.

i presume that was the problem...

Happy to help, good luck

This topic is closed to new replies.

Advertisement