Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi there code people,

So, I've been having this problem, where I have some objects of a struct I made, and I have a vector of pointers to these objects. This vector is then, passed to a function who's supposed to alter the values of this objects, but I can't help to find the vode to do it. Something like this

typedef struct _foo
{
   int bar;
   void foo(int i) bar = i;

} foo;

foo var1(1), var2(2), var3(3);
vector<foo*> vec;
vec.pushback(var1);
vec.pushback(var2);
vec.pushback(var3);




Then, in some other cpp file, what I'm doing is


void foobar(vector<foo*> vec)
{
foo* aux;
for (int i=0;vec.size(), ++i)
{
   aux = vec[i];
   *(aux).bar = 1; //that's where I don't know the sintax, and I can't find it
                   //anywhere. You probably understand what I mean with this pseudo
                   //sintax, I just want to alter the value in my struct, so to say,
                   //alter the value in the memory location of the object.
}
}



If you guys can help me with this, I would apreciate a lot.
EDIT: BBcode fixed
Thankz ;)
Posted

1 solution

Try
(*aux).bar = 1

or
aux->bar = 1;

btw i think here:
C#
vec.pushback(var1);
vec.pushback(var2);
vec.pushback(var3);

you mean
C#
vec.pushback(&var1);
vec.pushback(&var2);
vec.pushback(&var3);
 
Share this answer
 
Comments
Juuca 20-Apr-12 6:38am    
I would sear I have tried that --'

But yeah it's working now. And the pushback(&var), I did have &, but I pasted the code from another question I was writing(noticed I wasn't logged), and the & weren'trasnfered rigth --'.

Really appreciated ;)
Thankz
Code-o-mat 20-Apr-12 6:42am    
Yourwelcomz :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900