Vector of objects

I haven't used vectors much so I'm not sure if this problem is with how I'm implementing it or something else. This is supposed to loop through the checkboxes and check them until it reaches the limit set by acctNum. After the first checkbox get set, it goes to the start of the for loop, but instead of incrementing i it exits the function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  void Card::OnEntriesChecked(wxCommandEvent& WXUNUSED(event))
{
	long acctNum;

	std::vector<wxCheckBox*> R1cb;
	R1cb.push_back(m_cbR1H1);
	R1cb.push_back(m_cbR1H2);
	R1cb.push_back(m_cbR1H3);
	R1cb.push_back(m_cbR1H4);
	R1cb.push_back(m_cbR1H5);
	R1cb.push_back(m_cbR1H6);
	R1cb.push_back(m_cbR1H7);
	R1cb.push_back(m_cbR1H8);
	R1cb.push_back(m_cbR1H9);
	R1cb.push_back(m_cbR1H10);
	R1cb.push_back(m_cbR1H11);
	R1cb.push_back(m_cbR1H12);

	if(m_cbEntriesR1->GetValue() == true)
	{
		for(int i = 0; i < m_entriesR1Txt->GetValue().ToLong(&acctNum); ++i)
		{
			R1cb[i]->SetValue(true);
		}
	}
}


I don't think this is a wxWidgets issue as much as something I'm doing wrong with the vector since that seems to be where the problem lies. After VS exits the function I get "No Source Available" tab pop up that I've only ever seen when working with assembly and couldn't get a reason for that from my teacher. I have a feeling it's a memory issue of some kind, but not sure were to look. Any ideas would be a great help.
I changed it to
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void Card::OnEntriesChecked(wxCommandEvent& WXUNUSED(event))
{
	long acctNum;

	m_entriesR1Txt->GetValue().ToLong(&acctNum);

	std::vector<wxCheckBox*> R1cb;
	R1cb.push_back(m_cbR1H1);
	R1cb.push_back(m_cbR1H2);
	R1cb.push_back(m_cbR1H3);
	R1cb.push_back(m_cbR1H4);
	R1cb.push_back(m_cbR1H5);
	R1cb.push_back(m_cbR1H6);
	R1cb.push_back(m_cbR1H7);
	R1cb.push_back(m_cbR1H8);
	R1cb.push_back(m_cbR1H9);
	R1cb.push_back(m_cbR1H10);
	R1cb.push_back(m_cbR1H11);
	R1cb.push_back(m_cbR1H12);

	if(m_cbEntriesR1->GetValue() == true)
	{
		for(int i = 0; i < acctNum; ++i)
		{
			R1cb[i]->SetValue(true);
		}
	}
}


and I have that part working now. I still have what I think is a memory problem that involves the class, but for now this issue is resolved.
In the first snippet, your loop wasn't counting up to the value of acctNum, but to whatever the return value of ToLong() is, probably boolean. That would explain why the loop is executed just once.

I can't see anything wrong with the vectors, but make sure all the pointers you're pushing are initialised.
Hi tipaye, thanks for the reply. They both should be the same. m_entriesR1Txt is a text box and the value of acctNum is set to the value returned from ->GetValue().ToLong(). ToLong() requires a variable which I hadn't originally planned to use. The times before I did something similar was in Java and I didn't need a variable for the cast so the way I had it first would have worked. I don't know why I didn't think to switch out the variable in the for conditions to begin with but live and learn I guess.
Topic archived. No new replies allowed.