vector of struct doesn't save values

  • Follow


Hi there :)

I've defined a vector of struct in my header file :

struct cluster_member {
	int chnum;
	vector<int> members;
};

  protected:
	int updateClusMember(int cid,int membernodeid);

I have the following function in my .cc file:

int HybridClustering::updateClusMember(int cid,int membernodeid)
{
	trace()<<"initiaal size: "<<cluster_members_table.size();  //whenever
this function is called, the printed size is ZERO!
	int pos = -1;
	for (int i = 0; i < (int)cluster_members_table.size(); i++) {  //
first see if a cell containing the cid(input) already exists
		if (cluster_members_table[i].chnum == cid)
			pos = (int)cluster_members_table.size();
	}

	if (pos == -1) {
  		cluster_member clusmember;
		clusmember.chnum = cid;
   	        clusmember.members.clear();
  	        clusmember.members.push_back(membernodeid);
		cluster_members_table.push_back(clusmember);
                 trace()<<"secondry size:
"<<cluster_members_table.size(); //this size is always 1
	} else {
		cluster_members_table[pos].members.push_back(membernodeid);
		}
	return 0;
}

The problem is that, whenever this function is called, it seems that
the vector is empty and new data is written in the vector.Actually the
first (initial size) trace , always shows 0 in my log,however when I
call the updateClusMember() for the second time, the initial size
should be 1!The vector doesn't save anything!

What am I doing wrong?
Please let me now if more detail is needed.

Thanks in advance,
Fae


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply faaamemar (1) 3/17/2011 7:15:17 AM

On Mar 18, 8:09 am, Fred <fred.l.kleinschm...@boeing.com> wrote:
> On Mar 17, 6:15 am, Faeze HM <faaame...@gmail.com> wrote:
>
> >         if (pos == -1) {
> >                 cluster_member clusmember;
>
> clusmember is now a local variable that will go out of scope at the
> end of this 'if' block.

True, but harmless.

> >                 cluster_members_table.push_back(clusmember);

Now he has copied clusmember into cluster_members_table, and no longer
needs it.

The question is why cluster_members_table keeps losing its values -
and for that, we need to see more code.


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Martin 3/22/2011 2:31:09 PM


1 Replies
308 Views

(page loaded in 0.102 seconds)

Similiar Articles:













7/23/2012 2:39:30 AM


Reply: