Another Memory Access Exception!

Ok, so this time I'm working with a vector since cire pointed out that my lernin' there was wrong! I'm working on the constant problem over in http://www.cplusplus.com/forum/general/95710/. I went ahead and designed a class that acts as a constant containing an int and string. It can only be initialized using a constructor, and cant be changed except to be deleted. This ensures that the contents act as constants throughout most of the remainder of the program.

I'm overloading = for the container class. It's only private data member is a vector<Constant> named Const. Here's the operator's code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
CVector& CVector::operator= (const CVector& ToCopy)
{
     if( ToCopy.Const->empty() )
          return (*this);

     int i = 0;

     vector<Constant>::const_iterator Current = ToCopy.Const->begin();

     while( Current != ToCopy.Const->end() )
     {
          cout << *Current << ", " << i << endl;
          Const->push_back(*Current);
          Current++;
          i++;
     }

     Sort();

     return (*this);
}


Now, here's the code in Main():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <string.h>
#include <fstream.h>
#include "Constant.cpp"
#include "ConstantVector.cpp"

int main(int argc, char* argv[])
{
     CVector MyVec;

     Constant c1(1, "Thingy 1");
     Constant c2(2, "Whtchamagigger");
     Constant c3(3, "What's-it?");
     Constant c4(4, "Thingamabober 4");
     MyVec = MyVec + c1 + c2 + c3 + c4;

     cout << MyVec;

     getch();
     return 1;
}


I'm getting the error because the while loop in the = operator function isn't ending. This is similar to the loop that cire suggested for my first problem, and I had assumed that the iterators would behave the same for a vector as they did for a list. However, this is what the console looks like when the error gets thrown:

http://arolkay.com/CppPics/console.jpg

So it looks like the code is looping through the vector indefinitely until it reaches some upper limit where it finally throws the exception. I'm at a loss to figure out what's happening. Shouldn't the comparison in the While stop it when it reaches the 4th element?
Last edited on
Your operator= doesn't work when MyVec is assigned to itself.

To copy a std::vector you just need to use operator=, no need to loop.
*Const = *ToCopy.Const;
Why is Const a pointer by the way?
Last edited on
Err.... why not just do this?

1
2
3
4
5
CVector& CVector::operator= (const CVector& ToCopy)
{
   Const = ToCopy.Const;
   return *this;
}


Or for that matter... why implement this operator at all? The compiler provided default assignment operator will do exactly what that does. You don't have to code this manually.
Last edited on
Wow, same mistake I made before lol! I need to start assuming the library designers knew what they're were doing lol!

not sure why it's a pointer, and now that you point it out, I don't think that it needs to be.
not sure why it's a pointer, and now that you point it out, I don't think that it needs to be.
What Disch said only works if Const isn't a pointer.
Topic archived. No new replies allowed.