Oooops, yes I can. I forgot to add std:: in front of the vector (habit of using namespace std in console programs)phlox81 wrote:
Sure you can. I do that every day.
You cannot add them to the Widgets, but you can't do that eather with the wxObjArray.
The STL offers a lot of algorithms and accessiterators for its containers,
which makes me favor them.
How to create and store instances of a class at runtime Topic is solved
Oops, I didn't notice last night, but you're creating an array, not of heros, but of bureaucrats . It should have been WX_DECLARE_OBJARRAY(Hero, ArrayOfHeroes);Cheo wrote:Ok, now I've added this new class declaration into the header file:Code: Select all
class HeroManager; WX_DECLARE_OBJARRAY(HeroManager, ArrayOfHeroes); class HeroManager { public: ArrayOfHeroes m_Heroes; };
The following does compile:
Code: Select all
#include <wx/dynarray.h>
class Hero {
public:
int m_Strength;
int m_Intelligence;
int m_Dexterity;
int m_Endurance;
int m_Personality;
wxString m_Name;
Hero(wxString name, int strength, int intelligence,
int dexterity, int endurance, int personality);
};
WX_DECLARE_OBJARRAY(Hero, ArrayOfHeroes);
class HeroManager {
public:
ArrayOfHeroes m_Heroes;
};
Code: Select all
// In, e.g., the frame constructor
HeroManager manager;
manager.m_Heroes.Add( Hero("David", 100, 1, 1, 1, 1) );
...
}
Hero::Hero(wxString name, int strength, int intelligence, int dexterity,
int endurance, int personality) {
m_Name = name;
m_Strength = strength;
m_Intelligence = intelligence;
m_Dexterity = dexterity;
m_Endurance = endurance;
m_Personality = personality;
}
It does compile in the OnClick funtion of WxButton, which is what I wanted:
However, the above constructor doesn't compile outside the scope of any function of the Frame source code. But I guess this is a normal behaviour.
Thanks, I may say now problem solved.
Code: Select all
void HeroeFrm::WxButton1Click6(wxCommandEvent& event)
{
manager.m_Heroes.Add( Hero("David", 100, 1, 1, 1, 1) );
}
However, the above constructor doesn't compile outside the scope of any function of the Frame source code. But I guess this is a normal behaviour.
Thanks, I may say now problem solved.
Yes. If you want to do this elsewhere, then that function would need a pointer to the frame:However, the above constructor doesn't compile outside the scope of any function of the Frame source code. But I guess this is a normal behaviour.
pFrame->manager.m_Heroes.Add( Hero("David", 100, 1, 1, 1, 1) );
Though if that's where you want it done, you'd probably just put the manager instance there instead.
And wherever your HeroManager class is stored, you'll want it as a member, not a local as in my last post. So HeroManager manager; will be in the class declaration, not in the constructor.
I think he runs better if he implements this manager as a singleton, that wayDavidHart wrote:Yes. If you want to do this elsewhere, then that function would need a pointer to the frame:However, the above constructor doesn't compile outside the scope of any function of the Frame source code. But I guess this is a normal behaviour.
pFrame->manager.m_Heroes.Add( Hero("David", 100, 1, 1, 1, 1) );
Though if that's where you want it done, you'd probably just put the manager instance there instead.
And wherever your HeroManager class is stored, you'll want it as a member, not a local as in my last post. So HeroManager manager; will be in the class declaration, not in the constructor.
you can access it from any point in the application.
Also the array shouldn't be public, the control over the Hero Class instances belongs to the Manager, and no other class.
phlox
That's the intention. However, I'm having problems to access the data members once the instance is created.phlox81 wrote:I think he runs better if he implements this manager as a singleton, that way
you can access it from any point in the application.
Declaring std::vectors and a function to store instances is fine:
Code: Select all
class HeroManager {
public:
void Add(const Hero& aHero) {m_Heroes.push_back(aHero);}
std::vector<Hero> m_Heroes;
std::vector<Hero>::iterator iter;
};
Code: Select all
wxString contador;
manager.Add(Hero(name, strength, intelligence, dexterity, endurance, personality));
int cont = manager.m_Heroes.size();
contador.Printf("%d",cont);
WxStaticText14->SetLabel(contador);
You don't need the iterator as a class member, the vector itself should be private.Cheo wrote: That's the intention. However, I'm having problems to access the data members once the instance is created.
Declaring std::vectors and a function to store instances is fine:Code: Select all
class HeroManager { std::vector<Hero> m_Heroes; public: void Add(const Hero& aHero) {m_Heroes.push_back(aHero);} std::vector<Hero>::const_iterator begin()const {return m_Heroes.begin();} std::vector<Hero>::const_iterator end()const {return m_Heroes.end();} };
Access etc. could and should be implemented in the Manager class itself,Cheo wrote: Then printing the number of intances created is fine:But what I don't know is how to access a data member.Code: Select all
wxString contador; manager.Add(Hero(name, strength, intelligence, dexterity, endurance, personality)); int cont = manager.m_Heroes.size(); contador.Printf("%d",cont); WxStaticText14->SetLabel(contador);
f.e. with an Editmember(int index) method, which launches a Dialog.