Hi, I created an array of objects, based on a class I created to hold data only, at the start of my program. During the run time, I may need to add an unknown number of additional objects to new data. Can I do this How can I increase an array of objects during run timge, one at a time as the need arises
For exmaple:
myClass aryObjects[10];
..............
How do I add to the aryObjects and make it a aryObjects[11]
Thanks.

Can and how do I extend an array of objects during run time?
blemmon
This is a List Control(ListView) with unknow number of listing until run time when I load it up and it could be a few listing or a couple hundred of listing...
Any way to dynamically add new object to anarray of objects(or sturct type variable)
Thanks.
IceX
userData =
new CUserContextData[numOfChecks];There is a listview(with checkboxes) control in my dialog window. When user checks additional listing item then I would need to create an additional userData object each time to hold the data coorsponding to that checked item. (Oh, I am yet to change from array to vector)
My question is how do I declare this new item I mean, I can't use the same name over and over now can I
pven
Hi, I've been working on other part of the project but this problem still exist for me. I was wondering, would vector store a class object that I defined which mainly to hold some data for me. I suspect it might be a sytax problem Becuase all the example I've seen on msdn using only the intergers like v.push_back(10);
How do I pass an object to it
userDataObjects.push_back(&newUserData);
That is not working becuase compiler comes back complaining that it expects
the arg to be of type :
error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from
'CUserContextData *' to 'const CUserContextData &'
p.s., my class object has several members that are declare as wchar_t*. So, it's not just a simple varible or a constant number.
Thank you.
tonyinbeijing
Can you not use conditional statements to name objects that correspond to each checkbox That would solve that problem I think.
i.e. if(checkbox1->selected==true)
Harendra Singh
I'm getting Assertion error with the following line of code. Can anyone see what I'm doing wrong here Thanks.
vector <CUserContextData> userDataObjects;
vector<CUserContextData>::iterator userDataIter;
userDataObjects.push_back(newUserData);
Joseph Kasende
Craig Guyer - MSFT
I don't quite understand your question, but this is how I'll answer it ;)
If you can do it for an int, then you can do it for vector...
lchase4411
karls123
Hi Thankk you for the reply. I open the #include<vector> and found
void push_back(const _Ty& _Val)Do I change it to : void push_back(const _Ty* _Val)
if not, then where and how do I change the template that you're referring to
Also, below is my class definition, statement of instantiate an object of that class and a smaple of how I allocate memory for one of the member data. Do I need to create a Copy menthod for my class, otherwise, how does vector knows how to make a copy of my data
Thank you.
class
CUserContextData{
public
:CUserContextData(
void){
}
virtual ~CUserContextData(void){
if(wcslen(shell) >0) delete [] shell; if(wcslen(homeDir) >0) delete [] homeDir; if(wcslen(primaryGroup) >0) delete [] primaryGroup; if(wcslen(symarkUserRef) >0) delete [] symarkUserRef; if(wcslen(LoginName) >0) delete [] LoginName;}
public
: bool newObject, bWinLogOn, bUnifiedID, symarkEnabled, UIDChanged; wchar_t *shell, *homeDir, *primaryGroup, *symarkUserRef, *LoginName, *contextName;LPCWSTR lpcwContextName;
int symarkUID, listViewIndex, UID;};
/////////////////////////////////////////////
CUserContextData *newUserData =
new CUserContextData;/////////////////////////////////////////////
//Get the Shell
hResult =ptSyUserObject->Get(CComBSTR(
"symark-Shell"), &var); if(FAILED(hResult))//New Sy User{
DisplayError( hResult, L
"Error obtaining symark-Shell. - RetrieveUserAtt"); goto CleanUp;}
temp = var.bstrVal;
if(temp.length()>0){
lpcTemp = temp;
newUserData->shell =
new wchar_t[wcslen(lpcTemp)+1];wcscpy(newUserData->shell, (lpcTemp));
}
elsenewUserData->shell =NULL;
VariantClear(&var);
mmmattias
Here's an example for you... I like examples when I'm not sure what to do...
//include this...
#include
<vector>std::vector<myClass> anyObjects;
//That's it to declare it!std::vector<myClass>::iterator aOi;
//this is what we're going to use to navigate through our vector//Adding objects to the vector. Push back adds the object to the end of the vector
anyObjects.push_back(myObject1);
anyObjects.push_back(myObject2);
anyObjects.push_back(myObject3);
//etc//A method of getting at the objects
for
(aOi = anyObjects.begin(); aOi!= anyObjects.end(); aOi++){
myTempObject = aOi;
}
//Another method of getting at the objects
myTempObject = anyObjects.front();
//gets the first object in vectormyTempObject = anyObjects.back();
//gets the last object in vectorI hope this helps...
Ljubica
Tim Greenwood
Hey, the template parameter type is different with the type of the object you passed to push_back, one is reference type, one is pointer type
remove the & in the push_back statement to pass reference.or you can change the template parameter of std::vector from CUserContextData& to CUserContextData*
grissett
Have you thought of using a linked list instead. That is to have the object contain a pointer to the next object that gets created at run time.
Thanks, Ayman Shoukry VC++ TeamDerek Lan
Cheers,
::Camelo
vito1281