vectors in a class within a class not working.

Im using a STD::VECTOR referenced in a class. I have a class CCSToBOM_AttribObj with some data in it.
I have another class (CCSToBOM_RefObj )with some data and a vector of CCSToBOM_AttribObj.
I have a top level class with some member fuctions to put data in and print out the results that also has a vector list of CCSToBOM_RefObj.
i.e A list of data, and each data also contains a list.
I have sucessfully filled the CCSToBOM_RefObj class and printed the data. i.e Member function CCSToBOM_Proc::AddRef works and the first part of CCSToBOM_Proc::Save works.

The problem is that the second part, adding the data for each Ref fails to work
i.e CCSToBOM_Proc::AddAttrib and the second part of CCSToBOM_Proc::Save does not work.
I see that in CCSToBOM_Proc::Save the itterator is not finding the CCSToBOM_AttribObj.

This I think is due to the fact that when I report the size of the list after adding an item it is still saying 1 within the CCSToBOM_Proc::AddRef code.
Commented out section tests the size.

Im confused how the outer loop works but the inner one does not.
It's probably a referencing issue??????


I have no idea whats wrong really.
I'm using visual c++ 2003.

Any pointers would be useful.


1
2
3
4
5
6
7
8
9
10
#include <vector>
class CCSToBOM_AttribObj
{
	public:
	CCSToBOM_AttribObj() ;
	virtual ~CCSToBOM_AttribObj() ;
	CString AttribType;
	CString AttribValue;
	//.. pointer to Next AttribObj.
};


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
class CCSToBOM_RefObj
{
	public:
	CCSToBOM_RefObj() ;
	virtual ~CCSToBOM_RefObj() ;

	CString RefName;
	CString PartName;
	CString Height;
	//private:
	std::vector<class CCSToBOM_AttribObj> m_AttribObj;
};

class CCSToBOM_Proc
{
	public:
	CCSToBOM_Proc();
	virtual ~CCSToBOM_Proc();

	void Save(CFile *file);
	void AddRef(CString addRef, CString addPart, CString Height);
	void AddAttrib(CString addType, CString addValue);
	private:
	std::vector<class CCSToBOM_RefObj> m_RefObj;
};



CODE.CPP
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "stdafx.h"
#include "CSToBOMProc.h"

CCSToBOM_Proc::CCSToBOM_Proc()
{
// Constructor Does nothing
}

CCSToBOM_Proc::~CCSToBOM_Proc()
{
// Destructor does nothing
}

void CCSToBOM_Proc::AddRef(CString addRef, CString addPart, CString Height)
{
	CCSToBOM_RefObj *myObj ;

	myObj = new CCSToBOM_RefObj;
	myObj->RefName = addRef;
	myObj->PartName = addPart;
	myObj->Height = Height;

	// Try insert way
	//m_RefObj.insert( m_RefObj.end( ), *myObj);

	this->m_RefObj.push_back( *myObj );

	/* // some debug stuff
	char buf[1024];
	std::vector<class CCSToBOM_RefObj>::size_type m;
	m = m_RefObj.size( );

	CFile fileTmp("C:\\tmp.txt", CFile::modeWrite);
	fileTmp.SeekToEnd();

	sprintf(buf, " Ref size=%d \r\n\r\n", m );
	fileTmp.Write(buf, strlen(buf));

	fileTmp.Close();
	*/
	// Delete original object as it's been put in the vector.
	delete (myObj);
}

void CCSToBOM_Proc::AddAttrib(CString addType, CString addValue)
{
	// Reference an itterator to the end of the REF components, note -1 as the end is a place holder for a new component in the list
	std::vector<class CCSToBOM_RefObj>::const_iterator i=this->m_RefObj.end() - 1;
	CCSToBOM_RefObj RefObj = *i;

	// Create a new structure of the attribute class
	CCSToBOM_AttribObj *myObjAtt = new CCSToBOM_AttribObj;

	myObjAtt->AttribType = addType;
	myObjAtt->AttribValue = addValue;

	// Don't think this line works as .Size returns just 1, never increments ???
	RefObj.m_AttribObj.push_back( *myObjAtt );

	// Try insert way.
	// Create an itterator for the end of the attribte list
	//std::vector<class CCSToBOM_AttribObj>::iterator n=RefObj.m_AttribObj.end();

	// Insert new data into the list at the end.
	//RefObj.m_AttribObj.insert( n, *myObjAtt);
	//RefObj.m_AttribObj.insert(RefObj.m_AttribObj.end(), *myObjAtt);

	/*
	// Some debug, size does not increas, stuck at one
	char buf[1024];
	std::vector<class CCSToBOM_AttribObj>::size_type m;
	m = RefObj.m_AttribObj.size( );

	CFile fileTmp("C:\\tmp.txt", CFile::modeWrite);
	fileTmp.SeekToEnd();
	////sprintf(buf, "RefObj=%d Attr=%d type=%s value=%s size=%d \r\n", RefObj, RefObj.m_AttribObj, addType, addValue , m );
	//sprintf(buf, "type=%s value=%s ", addType, addValue );
	//fileTmp.Write(buf, strlen(buf));

	//sprintf(buf, "RefObj=%d Attr=%d ", RefObj, RefObj.m_AttribObj );
	//fileTmp.Write(buf, strlen(buf));

	sprintf(buf, "Ref=%s ", RefObj.RefName );
	fileTmp.Write(buf, strlen(buf));

	sprintf(buf, "size=%d \r\n\r\n", m );
	fileTmp.Write(buf, strlen(buf));

	fileTmp.Close();
	*/

	// Delete the tmp structure as it's in the list now.
	delete (myObjAtt);

}


void CCSToBOM_Proc::Save(CFile *file)
{
	//int i;
	char buf[1024];

	// Loop round the references printing out the parts.
	std::vector<class CCSToBOM_RefObj>::const_iterator i=m_RefObj.begin();
	for (; i!=m_RefObj.end(); i++)
	{
		CCSToBOM_RefObj RefObj = *i;
		sprintf(buf, "%s, ", RefObj.RefName);
		file->Write(buf, strlen(buf));

		sprintf(buf, "%s, ", RefObj.PartName);
		file->Write(buf, strlen(buf));

		// Walk through the attributes in the list.
		std::vector<class CCSToBOM_AttribObj>::const_iterator n=RefObj.m_AttribObj.begin();

		for (; n!=RefObj.m_AttribObj.end(); n++)
		{
			CCSToBOM_AttribObj AttribObj = *n;

			sprintf(buf, "%s(%s), ", AttribObj.AttribType, AttribObj.AttribValue );
			file->Write(buf, strlen(buf));
		}

		file->Write("\r\n", 2);
	}

}



CCSToBOM_RefObj::CCSToBOM_RefObj()
{
// Constructor
}

CCSToBOM_RefObj::~CCSToBOM_RefObj()
{
// Destructor
}


CCSToBOM_AttribObj::CCSToBOM_AttribObj()
{
// Constructor
}

CCSToBOM_AttribObj::~CCSToBOM_AttribObj()
{
// Destructor
}



CODE.CPP CALLER BIT, NOT FULL CODE ETC

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
CCSToBOM_Proc iCCSToBOM_Proc;
	while (NextComp(&m_Comps, pDispatch) && statusOK)
	{
		partName = GetPartName(Comp);
		refName = GetName(Comp);
		iCCSToBOM_Proc.AddRef(refName, partName, string);

		while (NextAtt(&Attribs, pDispAttrib))
		{
			CString value;
			CString AttName;
			GetAttNameValue(&AttName, &value, pDispAttrib);
			if (CheckAttribRequired(attribListStr, AttName))
			{
				// Push Attribute to CStoBom_Proc()
				iCCSToBOM_Proc.AddAttrib(AttName, value);
			}
		}
	}



Thanks

Lee







Thanks

Lee
Sorry originally posted in wrong place
Last edited on
I'm going to do you a favor and make a suggestion. First, read this.
http://cplusplus.com/articles/how_to_ask/

Second, please format/beautify the code in your original editor. Copy and paste the code back into your original post. Highlight all of the code. Press the # button to the right of the edit box (this encloses it within code tags). Submit. Now your code will be readable. It's hard enough to read the question and figure out what you are asking but when the posted code is so unreadable many people will just skip by the post and not even bother with it.
Thanks, It is more readable by doing that, sorry first time, Case of new-by RTFM. Thanks

Lee
FIXED IT MYSELF:-

It was due to the use of const_iterator instead of an basic iterator and some referencing.

Code changed to:-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void CCSToBOM_Proc::AddAttrib(CString addType, CString addValue)
{

     // Reference an itterator to the end of the REF components, note -1 as the end is a place holder for a new component in the list
      std::vector<class CCSToBOM_RefObj>::iterator i=this->m_RefObj.end() - 1;
	  //CCSToBOM_RefObj RefObj = *i;

     // Create a new structure of the attribute class
      CCSToBOM_AttribObj *myObjAtt = new CCSToBOM_AttribObj;

      myObjAtt->AttribType  = addType;
      myObjAtt->AttribValue = addValue;

      // Don't think this line works as .Size returns just 1, never increments ???
      i->m_AttribObj.push_back( *myObjAtt );

      // Delete the tmp structure as it's in the list now.
      delete (myObjAtt);

}


Hope this helps someone else.

Topic archived. No new replies allowed.