Array of objects?
-
Is it possible to create an array of objects or an array of pointers to objects. I think the later is what I am really after. I have tried doing such a beast and all I get is an error.
class Bar { Bar(CString strName, CString strAddress) } class Foo { protected: Bar* m_poBars[16]; public: Foo(); Create(INT nIndex); } ////////// Implementation Foo::Foo() { for (INT i; i < 16; i++) { m_poBars[i] = NULL; // This is not liked at all } } // Then I would like to initialize an arbitrary location with the Bar class Foo::Create(INT nIndex) { m_poBars[nIndex] = new Bar("My Place", "123 Road"); }
The above code doesn't seem to be too happy X| and I was wondering what I am doing wrong. Cheers, Clint Singer
-
Is it possible to create an array of objects or an array of pointers to objects. I think the later is what I am really after. I have tried doing such a beast and all I get is an error.
class Bar { Bar(CString strName, CString strAddress) } class Foo { protected: Bar* m_poBars[16]; public: Foo(); Create(INT nIndex); } ////////// Implementation Foo::Foo() { for (INT i; i < 16; i++) { m_poBars[i] = NULL; // This is not liked at all } } // Then I would like to initialize an arbitrary location with the Bar class Foo::Create(INT nIndex) { m_poBars[nIndex] = new Bar("My Place", "123 Road"); }
The above code doesn't seem to be too happy X| and I was wondering what I am doing wrong. Cheers, Clint Singer
I think everything is perfect except the following line:
for (INT i; i < 16; i++)
You forgot to initialize
i
:for (INT i=0; i < 16; i++)
Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
-
Is it possible to create an array of objects or an array of pointers to objects. I think the later is what I am really after. I have tried doing such a beast and all I get is an error.
class Bar { Bar(CString strName, CString strAddress) } class Foo { protected: Bar* m_poBars[16]; public: Foo(); Create(INT nIndex); } ////////// Implementation Foo::Foo() { for (INT i; i < 16; i++) { m_poBars[i] = NULL; // This is not liked at all } } // Then I would like to initialize an arbitrary location with the Bar class Foo::Create(INT nIndex) { m_poBars[nIndex] = new Bar("My Place", "123 Road"); }
The above code doesn't seem to be too happy X| and I was wondering what I am doing wrong. Cheers, Clint Singer
Like Joaquin said it looks good except for the initialazation part of 'i'. However, if that does not resolve your problem what is the specific error message you are getting? That would help diagnose the problem. PLUS are you sure that is where the error is generated? Your example class BAR is defined such as:
class Bar
{
Bar(int strName, int strAddress);
};The problem with this is that Bar is now a private Constructor by default. You would have problems initializing this in your main function when it calls FOO create unless this is declared as a friend of the class, but that wasn't appearent in your example. Plus you may want to add to the end of your constructor { } so it would be an inline function or define it somewhere else or you may get a linker error. Hope this helps out...I don't even know what I'm talking about right now...need more coffee :| HomeNuke ---- "Nuke'd Your Home, Yet?" Run your own PostNuke based web server from home http://www.homenuke.com
-
Like Joaquin said it looks good except for the initialazation part of 'i'. However, if that does not resolve your problem what is the specific error message you are getting? That would help diagnose the problem. PLUS are you sure that is where the error is generated? Your example class BAR is defined such as:
class Bar
{
Bar(int strName, int strAddress);
};The problem with this is that Bar is now a private Constructor by default. You would have problems initializing this in your main function when it calls FOO create unless this is declared as a friend of the class, but that wasn't appearent in your example. Plus you may want to add to the end of your constructor { } so it would be an inline function or define it somewhere else or you may get a linker error. Hope this helps out...I don't even know what I'm talking about right now...need more coffee :| HomeNuke ---- "Nuke'd Your Home, Yet?" Run your own PostNuke based web server from home http://www.homenuke.com
Some mistakes on my part. The Bar() constructor is infact public: and INT i is indeed INT i = 0; They are just wrong on my posting, sorry. I am still getting the error. To add more to the story I am making a simple ActiveX control and running it in visual basic. The errors I get are as such when I try to do m_poBars[i] = NULL; First-chance exception in VB6.EXE (DVMS CLIENT LIB.DLL): 0xC0000005: Access Violation. First-chance exception in VB6.EXE (KERNEL32.DLL): 0x80010105: (no name).
-
Some mistakes on my part. The Bar() constructor is infact public: and INT i is indeed INT i = 0; They are just wrong on my posting, sorry. I am still getting the error. To add more to the story I am making a simple ActiveX control and running it in visual basic. The errors I get are as such when I try to do m_poBars[i] = NULL; First-chance exception in VB6.EXE (DVMS CLIENT LIB.DLL): 0xC0000005: Access Violation. First-chance exception in VB6.EXE (KERNEL32.DLL): 0x80010105: (no name).
So the error you are getting is from the client side? Oh OK I'm thinking from a straight C++ side application :) Let me review this some more...sorry for not understanding the question in the first place. HomeNuke ---- "Nuke'd Your Home, Yet?" Run your own PostNuke based web server from home http://www.homenuke.com
-
Some mistakes on my part. The Bar() constructor is infact public: and INT i is indeed INT i = 0; They are just wrong on my posting, sorry. I am still getting the error. To add more to the story I am making a simple ActiveX control and running it in visual basic. The errors I get are as such when I try to do m_poBars[i] = NULL; First-chance exception in VB6.EXE (DVMS CLIENT LIB.DLL): 0xC0000005: Access Violation. First-chance exception in VB6.EXE (KERNEL32.DLL): 0x80010105: (no name).
I'd try to reproduce the error in a VC++-only application, without all the ActiveX mess. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
-
I'd try to reproduce the error in a VC++-only application, without all the ActiveX mess. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
I tried it with a MFC exe dialog and it seems to work just as I had expected it too. I guess I will have to do some more trial and error to find the problem. Cheers, Clint Singer
-
I think everything is perfect except the following line:
for (INT i; i < 16; i++)
You forgot to initialize
i
:for (INT i=0; i < 16; i++)
Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
I can't believe it. The INT i... Well it was the problem. I made the mistake in my posting and in my code. What is worse, the posting was totally made up to simplify the problem. I don't know how many times I looked at my real code and didn't notice that I wasn't initalizing i to Zero. Boy, I feel silly :laugh: Cheers, Clint Singer
-
I can't believe it. The INT i... Well it was the problem. I made the mistake in my posting and in my code. What is worse, the posting was totally made up to simplify the problem. I don't know how many times I looked at my real code and didn't notice that I wasn't initalizing i to Zero. Boy, I feel silly :laugh: Cheers, Clint Singer
Didn't you get a warning? The compiled should have issued one on this. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
-
Some mistakes on my part. The Bar() constructor is infact public: and INT i is indeed INT i = 0; They are just wrong on my posting, sorry. I am still getting the error. To add more to the story I am making a simple ActiveX control and running it in visual basic. The errors I get are as such when I try to do m_poBars[i] = NULL; First-chance exception in VB6.EXE (DVMS CLIENT LIB.DLL): 0xC0000005: Access Violation. First-chance exception in VB6.EXE (KERNEL32.DLL): 0x80010105: (no name).
If you're runnign in ActiveX from VB, then how do you know what line is causing the problem???? Sorry to dissapoint you all with my lack of a witty or poignant signature.
-
If you're runnign in ActiveX from VB, then how do you know what line is causing the problem???? Sorry to dissapoint you all with my lack of a witty or poignant signature.
And you should probably check that your arg to the create function is in bounds. Sorry to dissapoint you all with my lack of a witty or poignant signature.