Issue with pointer to a vector

Hello Friends,

I am a Java programmer and c++ is new to me. I have an issue with a pointer, in the code below i try to iterate over a list and push the pointer to the vector.

1
2
3
for(finalNodeList_iter = finalNodeList.begin(); finalNodeList_iter != finalNodeList.end(); finalNodeList_iter++)
{																		strcpy(m_currentAgentInfo->msn, (char *)(*finalNodeList_iter).c_str());									m_AgentInfoVector.push_back(m_currentAgentInfo);										
}


But when i iterate over the vector to see the value, i dont see all the values pushed in the vector. I just shows one value several time(size of the m_AgentInfoVector).

 
int vec_size = m_AgentInfoVector.size();										for (int agentIdx = 0; agentIdx < vec_size; agentIdx++)										{	    												DEBUG_Printf(Detail"====value in the vector is =[%s]", (m_AgentInfoVector[agentIdx])->msn);										}



what can be the issue here, one thing i can think of is i am pushing same pointer to the vector. But i am not sure how to solve this? Help or suggestion will be really helpful.

Regards
Adi
Last edited on
That looks like you're mixing C and C++. Also, I'm not sure why you have your code so spaced out. I'll repost it below:
1
2
3
4
5
for(finalNodeList_iter = finalNodeList.begin(); finalNodeList_iter != finalNodeList.end(); finalNodeList_iter++)
{
    strcpy(m_currentAgentInfo->msn, (char *)(*finalNodeList_iter).c_str());
    m_AgentInfoVector.push_back(m_currentAgentInfo);			
}


1
2
3
4
int vec_size = m_AgentInfoVector.size();
for (int agentIdx = 0; agentIdx < vec_size; agentIdx++) {
    DEBUG_Printf(Detail"====value in the vector is =[%s]", (m_AgentInfoVector[agentIdx])->msn);
}
Please, format your code that it will be visible on the screen without scrolling to the right.
Last edited on
I apologize for the spacing.
oh man, you messed the format big time...

on line 4 (from ciphermagi) you push always the same pointer to the vector. So the vector is filled with the same information.

if you want a copy then m_AgentInfoVector.push_back(*m_currentAgentInfo); // note the '*' and remove the '*' form your vector definition
thanks :coder777, i did realize that i always push same pointer to the vector. I can-not remove the "*" from my vector definition as other components in the code use it. Is there some other way i could resolve this??
Any suggestion???
doesn't ` finalNodeList_iter->c_str() ' do the same thing as ` (*finalNodeList_iter).c_str() ' ?
Any suggestion how i can avoid pushing same pointer to the vector.
You can do so: m_AgentInfoVector.push_back(new AgentInfo(*m_currentAgentInfo));

You must delete it later when it's not longer used otherwise you have memory leak
First of all I do not understand this code

1
2
3
for(finalNodeList_iter = finalNodeList.begin(); finalNodeList_iter != finalNodeList.end(); finalNodeList_iter++)
{																		strcpy(m_currentAgentInfo->msn, (char *)(*finalNodeList_iter).c_str());									m_AgentInfoVector.push_back(m_currentAgentInfo);										
}


Why do you use casting to char * (*finalNodeList_iter).c_str()? By default c_str() returns const char *

Also as already it was said you push_back the same object m_currentAgentInfo in the vector. What suggestings are you waiting?! It looks like ypu do not understand what you are doing. If you do not understand what you are doing how do we can help you?!

I am new to c++ and my first post here. I am tying to learn. Yes that casting is un-necessary.
After i have done the push_back of element on to the vector and then try to iterate over that vector, it just return last element pushed. The problem is that i always push the same pointer to the vector, how can i avoid it.
So for example i pushed values a, b and c into the vector and when i iterate over the vector to get the values, i just get "c" 3 times. Hope that makes things better.

@coder777: what is AgentInfo here??
Topic archived. No new replies allowed.