-
Help using Vectors
Hey,
I want to fill a vector usign the following code:
Code:
for(i=0; i<FAD.nExpected; i++)
{
pGetEdit[i] = (CEdit*) GetDlgItem(i+100);
int len = pGetEdit[i]->LineLength();
pGetEdit[i]->GetLine(1,str.GetBuffer(len), len);
if(len != 0)
vars.vectParmSelected.push_back(str);
}
aside from the visual part, the coe fails at run-time saying that the memory could not be read if I have the if statement. If I remove the if statement and copy every entry without checking into the vector it works fine. len represents the length of a line copied into str. So if there is nothign there len=0 and I don't want to copy it into the vector.
Is there another way to do it? or what am I doing wrong?
-
This is a problem with your CString. Do you need to call ReleaseBuffer? Is your vector holding CStrings or LPTSTR or something else - because if it is holding a pointer then there will be a problem when the value of the pointer is changed.
-
I actually rewrote that vector type. Here's the new thing, and sorry I forgot to post the variable def's.
Code:
structLink templink;
CEdit* pGetEdit[512];
for(i=0; i<FAD.nExpected; i++)
{
pGetEdit[i] = (CEdit*) GetDlgItem(i+100);
int len = pGetEdit[i]->LineLength();
pGetEdit[i]->GetLine(1,templink.ParamName.GetBuffer(len), len);
templink.Index = i;
if(len != 0)
vars.vectParmSelected.push_back(templink);
}
where
Code:
struct structLink
{
CString ParamName;
int Index;
};
class gVars
{
public:
std::vector < structBafParam > vectBP;
std::vector <structLink> vectParmSelected;
char hEngName[13];
..............
It only fails if len ever has a vlue of 0.
-
Everything with your vector is fine.
From MSDN: If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString member functions.
Where do you call ReleaseBuffer?
Also, if len is 0, why are you even calling GetLine? Will GetBuffer even work with 0 specified as the length of the buffer to get?