iterator find is not working on linux but same is working on windows Please help!

This is a discussion on iterator find is not working on linux but same is working on windows Please help! within the C++ Programming forums, part of the General Programming Boards category; I have created a sample program, I am able build on windows but on linux getting below error Code: TEST.HPP ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    40

    iterator find is not working on linux but same is working on windows Please help!

    I have created a sample program, I am able build on windows but on linux getting below error

    Code:
    TEST.HPP
    
    
    #include<iostream>
    #include <iterator>
    #include <vector>
    typedef int Int;
    
    
    using namespace std;
    
    
    template<class C, typename T>
    
    class poly_iterator : public iterator_traits<typename C::iterator>
    {
    private:
    	// typedef poly_iterator<C,T> _Self;
    
    protected:
    	typedef typename C::iterator I;
    	//!< regular iterator of the underlying container
    	I i; 
    	//! mimic inheritage from the regular iterator
    	//! because we cannot simply derive from it since
    	//            some iterators are regular pointers
    public:
    	operator I&() 
    	{
    		return (I&)i;
    	}
    	//!<needed to use poly_iterator in algorithms transparently
    
    public:
    	poly_iterator<C,T>() {};
    	poly_iterator<C,T>(const I& p):i(p) {};
    	//!< construction form const_poly_iterator
    
    	T& operator*() {return (T&)*i;}
    	T* operator->() {return (T*)&*i;}
    
    public: // compatibility with I iterator
    	poly_iterator<C,T>& operator++() {++i; return *this;}
    	poly_iterator<C,T>& operator--() {--i; return *this;}
    
    	poly_iterator<C,T> operator+(const Int count)
    	{
    		I temp = i; 
    		temp += count;
    		return poly_iterator<C,T>(temp);
    	}
    
    	poly_iterator<C,T> operator-(const Int count)
    	{
    		I temp = i; 
    		temp -= count;
    		return poly_iterator<C,T>(temp);
    	}
    
    	poly_iterator<C,T>& operator++(Int) 
    	{
    		i++; 
    		return *this;
    	}
    
    	poly_iterator<C,T>& operator--(Int)
    	{
    		i--;
    		return *this;
    	}
    
    	bool operator<(const poly_iterator<C,T>& other) const
    	{
    		return i < other.i;
    	}
    
    	bool operator<(const I& other) const
    	{
    		return i < other;
    	}
    
    	bool operator<=(const poly_iterator<C,T>& other) const
    	{
    		return i <= other.i;
    	}
    
    	bool operator<=(const I& other) const
    	{
    		return i <= other;
    	}
    
    	bool operator>(const poly_iterator<C,T>& other) const
    	{
    		return i > other.i;
    	}
    
    	bool operator>(const I& other) const
    	{
    		return i > other;
    	}
    
    	bool operator>=(const poly_iterator<C,T>& other) const
    	{
    		return i >= other.i;
    	}
    
    	bool operator>=(const I& other) const
    	{
    		return i >= other;
    	}
    
    	bool operator==(const I& other) const 
    	{   
    		return i==other;
    	}
    
    	bool operator!=(const I& other) const 
    	{
    		return i!=other;
    	}
    
    	bool operator==(const poly_iterator<C,T>& other) const 
    	{
    		return i==other.i;
    	}
    
    	bool operator!=(const poly_iterator<C,T>& other) const 
    	{
    		return i!=other.i;
    	}
    
    }; 
    
    TEST.CPP
    
    
    //#include "stdafx.h"
    // iterator_traits.cpp
    // compile with: /EHsc
    #include <iostream>
    #include <iterator>
    #include <vector>
    #include <list>
    #include <algorithm>
    
    using namespace std;
    
    #include "test.hpp"
    
    
    int main()
    {
        vector<int> v(10);
        v[4] = 42;
        poly_iterator<vector<int>, int> it;
    	poly_iterator<vector<int>, int> first(v.begin());
    	poly_iterator<vector<int>, int> last(v.end());
    	
    	it = find(first, last, 42);
        if (it != last) {
            cout << *it << '\n';
        }
    
    	return 0;
    }
    error:

    epuser@VOPTENG01 ~/Consolidator/vaibhav $> g++ test.cpp -o test.exe
    /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h: In function ‚_RandomAccessIterator std::__find(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, std::random_access_iterator_tag) [with _RandomAccessIterator = poly_iterator<std::vector<int, std::allocator<int> >, int>, _Tp = int]‚:
    /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:316: instantiated from ‚_InputIterator std::find(_InputIterator, _InputIterator, const _Tp&) [with _InputIterator = poly_iterator<std::vector<int, std::allocator<int> >, int>, _Tp = int]‚
    test.cpp:20: instantiated from here
    /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:204: error: no match for ‚operator-‚ in ‚__last - __first‚
    test.hpp:50: note: candidates are: poly_iterator<C, T> poly_iterator<C, T>erator-(Int) [with C = std::vector<int, std::allocator<int> >, T = int]
    /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_bvector.h:182: note: ptrdiff_t stderator-(const std::_Bit_iterator_base&, const std::_Bit_iterator_base&)
    /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:316: instantiated from ‚_InputIterator std::find(_InputIterator, _InputIterator, const _Tp&) [with _InputIterator = poly_iterator<std::vector<int, std::allocator<int> >, int>, _Tp = int]‚
    test.cpp:20: instantiated from here
    /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:225: error: no match for ‚operator-‚ in ‚__last - __first‚
    test.hpp:50: note: candidates are: poly_iterator<C, T> poly_iterator<C, T>erator-(Int) [with C = std::vector<int, std::allocator<int> >, T = int]
    /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_bvector.h:182: note: ptrdiff_t stderator-(const std::_Bit_iterator_base&, const std::_Bit_iterator_base&)

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need a operator- that takes an iterator as the right-hand-side. You only have an Int version.

    --
    mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I thought this question was already answered in your other thread.

    There are two subtraction operations on pointers: pointer - pointer = distance (int) and pointer - int = pointer. Random access iterators model pointers.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    40
    Hi Anon,

    It was not answered, As you suggested me to do "sample program".
    I tried that but it is clearly have some problem with find function.
    Please run the same program in linux, and please let me know what can be done?

    Thanks a lot for your kind assistance.

    Vaibhav

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Both me and Adak have answered the question NOW (whether it was answered before or not). You need a "distance between iterators".

    The reason you DON'T in Windows is probably due to the implementation of the STL library for vector and it's iterator - in the Windows variant it obviously compares if the iterators are equal, and Linux it subtracts one vector from the other (presumably to see if it's zero).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    40
    Hi,

    I am very new to linux, could not understand much.I am working on porting project from windows to linux. can you point me out in code where the necessary action is required?

    thanks,

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by vaibhavs17 View Post
    Hi,

    I am very new to linux, could not understand much.I am working on porting project from windows to linux. can you point me out in code where the necessary action is required?

    thanks,
    it is nothing to do with linux
    write operator- with accept iterator and returns int
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    And then, operator- should return the distance of two iterators: that's an integral type:

    Code:
        typename I::difference_type operator-(const poly_iterator<C,T>& other)
        {
            //I temp =i;
            //poly_iterator<C,T> temp =(temp -(other.i));
            //return temp;
            return i - other.i;
        }
    The implementation of std::find apparently is doing some loop unrolling for random access iterators and needs the distance for that.
    The latter is what the Windows port of GCC does but it gives the same error message (you can look at the place where the error occurs). The thing is that there is a specialization of the find algorithm for random access iterators which appear to be doing loop unrolling (looking at something like 4 items during one iteration) and needs to calculate the distance between iterators for that.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vaibhavs17 View Post
    Hi,

    I am very new to linux, could not understand much.I am working on porting project from windows to linux. can you point me out in code where the necessary action is required?

    thanks,
    As has been explained, it is not the difference between Linux and Windows EXACTLY that is the problem - it is a case of how std::find() is implemented in the two C libraries. You may find that if you use a different compiler in Windows, it also gives this error. Or if you move to Solaris, VMS or some other OS that by implication doesn't use the same compiler.

    And how to fix that has been posted several times by now.

    --
    Mast
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    40
    Thanks guys!
    You guys are really great!
    I have done the changes accordingly.
    It is working.
    I tried below code:
    Code:
    Int operator-(const poly_iterator<C,T>& others)
    	{
    		I temp = i; 
    		return	temp- others.i;
    		//temp -= count;
    		//return (temp);
    	}
    Thanks again for you help
    vaibhav

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-07-2009, 11:31 AM
  2. Port app from Windows to Linux
    By BobS0327 in forum Linux Programming
    Replies: 12
    Last Post: 02-12-2006, 01:35 AM
  3. FlashWindowEx not declared?
    By Aidman in forum Windows Programming
    Replies: 3
    Last Post: 05-17-2003, 02:58 AM
  4. Linux Under Windows
    By Strut in forum Linux Programming
    Replies: 3
    Last Post: 05-27-2002, 08:09 PM
  5. linux vs. windows
    By muttski in forum Linux Programming
    Replies: 18
    Last Post: 04-07-2002, 09:03 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21