Re: std::vector and object errors
Replies inline.
Benry wrote:
Benry:
1. Assignment operator and copy constructor should always take a const
reference argument; otherwise you cannot copy a constant object. The
correct syntax is as given by David Webber.
Fixed, and will note for the future.
2. In your posted code, why are you creating tjtemp on the heap? You are
storing objects in your vector, not pointers. Surely your code is
leaking memory?
I am deleting the pointer after it's being used, so there are no leaks.
I'm using new because of the constructor, and it's been that way since
I started? I don't know I guess.
Well, there was no delete in your posted code. There is no need to
create on the heap. You can do like this:
CString sTemp = "test string";
CMyType tjTemp(sTemp);
tjTemp.SetLinkLevel(NO_LINK);
m_vMyType.push_back(tjTemp);
3. It is not clear why you need to specify your own assignment operator
and copy constructor here. Doesn't the default one (member-wise copy) do
what you want?
There are two error messages, and using my own assignment operator and
copy constructor are what was needed to fix the issue. My program
works now. Thank you.
Well, you do not show your entire CMyType class defintion, but from what
I have seen you should not have to provide your own versions of
assignment and copy constructor. For example, if your copy cosntructor does
CMyType:: CMyType(const CMyType &rhs)
{
vFields = rhs.vFields;
sTableName = rhs.sTableName;
}
or better
CMyType:: CMyType(const CMyType &rhs):
vFields(rhs.vFields),
sTableName(rhs.sTableName)
{
}
then this is just what the default does, so you should not need it.
BTW, the reason your loop did not work was because you did
std::vector <CString>::iterator v_Iter;
where you should have done
std::vector <CMyType>::iterator v_Iter;
David Wilkinson