C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-30-2009, 02:51 AM   #1
Registered User
 
Join Date: Mar 2009
Posts: 35
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&)
vaibhavs17 is offline   Reply With Quote
Old 03-30-2009, 03:04 AM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 03-30-2009, 03:13 AM   #3
The larch
 
Join Date: May 2006
Posts: 3,082
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.

Quote:
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).
anon is offline   Reply With Quote
Old 03-30-2009, 03:18 AM   #4
Registered User
 
Join Date: Mar 2009
Posts: 35
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
vaibhavs17 is offline   Reply With Quote
Old 03-30-2009, 03:21 AM   #5
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 03-30-2009, 03:24 AM   #6
Registered User
 
Join Date: Mar 2009
Posts: 35
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,
vaibhavs17 is offline   Reply With Quote
Old 03-30-2009, 03:32 AM   #7
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
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
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 03-30-2009, 03:52 AM   #8
The larch
 
Join Date: May 2006
Posts: 3,082
Quote:
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.

Quote:
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).
anon is offline   Reply With Quote
Old 03-30-2009, 03:58 AM   #9
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 03-30-2009, 04:09 AM   #10
Registered User
 
Join Date: Mar 2009
Posts: 35
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
vaibhavs17 is offline   Reply With Quote
Reply

Tags
code

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Ping and Traceroute not working in IPv6 environment on Windows XP / Win Server 2003 vigneshp Networking/Device Communication 8 05-07-2009 11:31 AM
Port app from Windows to Linux BobS0327 Linux Programming 12 02-12-2006 02:35 AM
FlashWindowEx not declared? Aidman Windows Programming 3 05-17-2003 02:58 AM
Linux Under Windows Strut Linux Programming 3 05-27-2002 08:09 PM
linux vs. windows muttski Linux Programming 18 04-07-2002 09:03 PM


All times are GMT -6. The time now is 03:34 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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