Thread: Vector iterators incompatible

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    11

    Vector iterators incompatible

    Hello,

    First, let me state that I have solved the issue that was giving me the VC++ debug assertion: "Expression: vector iterators incompatible". It's just that I'm not exactly how my change made a difference.

    I have a 2-d array template class which as its only member has a vector-of-vectors. An abbreviated version is shown below:

    Code:
    template <class T>
    class vector2
    {
    protected:
        vector<vector<T> > elements;
    
    public:
        vector2();	
        vector2(unsigned rows, unsigned columns);
        vector2(unsigned rows, unsigned columns, const T& value);
        vector2(const vector2<T>& other);
    
        //Overloaded operators
        vector2<T>& operator=(const vector2<T>& other);
        vector<T>&  operator[] (unsigned index);
        vector<T>   operator[] (unsigned index) const;
    
        // ...other member methods...
    };
    The problem occurred in the implementation of the copy constructor body:

    Code:
    template <class T>
    vector2<T>::vector2(const vector2<T>& other) :
    elements(other.elements)
    {
    }
    ...which usually worked, but I subsequently changed it to:

    Code:
    template <class T>
    vector2<T>::vector2(const vector2<T>& other)
    {
    	elements = other.elements;
    }
    ...and VC++ no longer asserted. Thus, it appears the incompatible vector iterators has something to do copy-constructing a std::vector-of-std::vectors, rather than using the assignment operator. Can anyone explain why exactly this occurs? I'm not a newbie but I'm also not an expert, so this might be a little beyond my current understanding. Normally, using the copy constructor works just fine - each vector-of-vector elements are always copied as I expect. It's just this specific assertion that I've never run into before.

    Thanks in advance!

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Sounds like a VC++ bug to me...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    11
    Judging by similar posts elsewhere (none exactly similar to my situation), it appears to be an issue with iterator stuff specifically used in DEBUG mode. If one were to ask Microsoft, I'm sure they'd tell you it was "by design." Not that I would argue - I'd just like to know what's going on behind the scenes, and why my first version would cause an assertion. If nobody knows, that's fine, I'm just curious.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Can you make a compilable example that shows the error (other than the entire source)? My instinct says "difference in const" but it's hard to say.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Why are you writing a copy constructor and assignment operator in the first place? The default ones already do what you want.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    11
    Quote Originally Posted by brewbuck View Post
    Why are you writing a copy constructor and assignment operator in the first place? The default ones already do what you want.
    Good point. I should note that originally, this class did not use std::vector internally, and those are relics from the old code I should have just eliminated. Had I done that in the first place, I'd never have run into this issue.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Incompatible Pointer Type Errors
    By Natta in forum C Programming
    Replies: 5
    Last Post: 09-01-2009, 02:33 PM
  2. Incompatible Pointer Type warnings
    By trillianjedi in forum C Programming
    Replies: 3
    Last Post: 06-11-2008, 04:16 PM
  3. Replies: 5
    Last Post: 08-12-2007, 05:26 PM
  4. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  5. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM