|
|
vector of struct doesn't save values
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: comp.lang.c++.moderatedvector of struct doesn't save values 1 211 (3/17/2011 7:15:17 AM) Hi there :) I've defined a vector of struct in my header file : struct cluster_member { int chnum ... Load data/.mat-file in Simulink - comp.soft-sys.matlabThis is held in an array of structures ... workspace' block in Simulink doesn't work. This appears to only apply to a vector of values ... load('my_data_file.mat') save('my ... map of string to ostringstream - comp.lang.c++.moderated ...... ve got an array of C structures with string names and values, but ... if insert succeeded, save iterator in traversal order vector ... them by shared pointer, it won't ... basic user-defined structure question - comp.lang.c++.moderated ...... GUARD_squares_h #define GUARD_squares_h struct ... std::cout; using std::vector; int main { vector ... return 0; } _____ This doesn't ... class definition containing member of own type - comp.lang.c++ ...The vector doesn't actually have any member that ... with type related to the vector's value ... Put it this way: struct MyClass { MyClass a; }; Now, this won't work as we need ... nitializing a static vector <> of integers (this static vectorBut if I understand correctly, he doesn't want to change the size of the vector at ... have many other advantages which save ... struct with const member variable without ... How to set the seed as a 128 binary string of psudo random number ...... binary string (eg. 11001101) That doesn't make ... I think the allowable values for a seed are ... If you save the 35-dim state vector at the end of an experiment you can ... Help with discrete double integral - comp.soft-sys.matlab ...That's nice, but it doesn't answer even one of the ... Let Y be a column vector of monotone y values for which Y(1) = c and Y ... Very weird: Conversion to double from struct is ... Static const integral data members can be initialized? - comp.lang ...The compiler can substitute the literal value as an ... off the cuff, template< class Dummy > struct ... you'll only get the linker error if the compiler doesn't ... crop image by polygon - comp.soft-sys.matlabI agree I can crop it that way, but it doesn't solve my ... message = sprintf('Mean value within drawn area = %.3f ... im_crop is your image vertex_x is the vector of x ... vector of struct doesn't save values C++/VBHi there :) I have defined a vector of struct in my header file : struct cluster_member { int chnum; vectorint members; }; protected: int updateClusMem Vector Of Struct - C And C++ | Dream.In.Code... and strings from a file and save ... #include <string> #include <vector> struct score { string name; int value ... He doesn't need the struct, end of story. "You see, a ... 7/23/2012 2:39:30 AM
|
|
|
|
|
|
|
|
|