![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Registered User Join Date: Mar 2009
Posts: 35
| iterator find is not working on linux but same is working on windows Please help! 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;
}
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 std erator-(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 std erator-(const std::_Bit_iterator_base&, const std::_Bit_iterator_base&) |
| vaibhavs17 is offline | |
| | #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 | |
| | #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:
| |
| anon is offline | |
| | #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 | |
| | #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 | |
| | #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 | |
| | #7 | |
| CSharpener Join Date: Oct 2006
Posts: 5,242
| Quote:
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 | |
| | #8 | ||
| The larch Join Date: May 2006
Posts: 3,082
| Quote:
__________________ I might be wrong. Quote:
| ||
| anon is offline | |
| | #9 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
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 | |
| | #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);
}
vaibhav |
| vaibhavs17 is offline | |
![]() |
| Tags |
| code |
| Thread Tools | |
| Display Modes | |
|
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 |