First of all, sorry for my English.
I keep getting weird compile errors for my programs which Borland C++ 5.5 compiles with no single warning. For example (199 Partial Differential Equations):
[cpp]
#include <algorithm>
#include <iostream>
#include <valarray>
#include <numeric>
using namespace std;
template <class T> class slice2d_iterator{
valarray<T>& data;
int strt, s1, s2, l1, l2, curr1, curr2;
void setcurr(int c1, int c2){curr1=c1; curr2=c2;}
public:
slice2d_iterator(valarray<T>& arr, int start, int stride1, int stride2,
int len1, int len2) : data(arr), strt(start), s1(stride1),
s2(stride2), l1(len1), l2(len2), curr1(0), curr2(0) {}
slice2d_iterator<T> end(){
slice2d_iterator<T> t(data, strt, s1, s2, l1, l2);
t.setcurr(l1, 0);
return t;
}
slice2d_iterator<T>& operator++(){
curr2++;
if(curr2==l2){curr2=0; curr1++;}
return *this;
}
slice2d_iterator<T> operator++(int){
slice2d_iterator<T> t = *this;
t.setcurr(curr1, curr2);
curr2++;
if(curr2==l2){curr2=0; curr1++;}
return t;
}
T& operator*(){return data[strt+curr1*s1+curr2*s2];}
bool operator == (slice2d_iterator<T>& k){
return curr1==k.curr1 && curr2==k.curr2 && strt==k.strt && s1==k.s1 &&
s2==k.s2 && l1==k.l1 && l2==k.l2;
}
};
template <class T> inline bool operator != (slice2d_iterator<T> k1, slice2d_iterator<T> k2){
return !(k1==k2);
}
template<class T> inline slice2d_iterator<T> submatrix(valarray<T>& data,
int top, int left, // top-left corner of the result
int rows, int cols, // dimensions of the result
int srcrowlen){ // number of elements in src matrix row
return slice2d_iterator<T>(data, top*srcrowlen+left, srcrowlen, 1, rows, cols);
}
/* ... */
[/cpp]
Online Judge wrote:
Here are the compiler error messages:
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/../../../../include/g++-3/stl_numeric.h: In
function `int inner_product<slice2d_iterator<long int>, long int *, int>(slice2d_iterator<long
int>, slice2d_iterator<long int>, long int *, int)':
01783980_24.c:77: instantiated from here
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/../../../../include/g++-3/stl_numeric.h:58:
ambiguous overload for `slice2d_iterator<long int> & != slice2d_iterator<long int> &'
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/../../../../include/g++-3/stl_relops.h:37:
candidates are: bool operator !=<slice2d_iterator<long int> >(const slice2d_iterator<long int> &,
const slice2d_iterator<long int> &)
01783980_24.c:41: bool operator !=<long int>(slice2d_iterator<long int>,
slice2d_iterator<long int>)
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/../../../../include/g++-3/stl_algo.h: In
function `void (* for_each<slice2d_iterator<long int>, void (*)(long int)>(slice2d_iterator<long
int>, slice2d_iterator<long int>, void (*)(long int)))(long int)':
01783980_24.c:92: instantiated from here
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/../../../../include/g++-3/stl_algo.h:82:
ambiguous overload for `slice2d_iterator<long int> & != slice2d_iterator<long int> &'
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/../../../../include/g++-3/stl_relops.h:37:
candidates are: bool operator !=<slice2d_iterator<long int> >(const slice2d_iterator<long int> &,
const slice2d_iterator<long int> &)
01783980_24.c:41: bool operator !=<long int>(slice2d_iterator<long int>,
slice2d_iterator<long int>)
--
If only I understood it right, B. Stroustrup wrote in "C++ Programming Language" (Special Edition, 17.1.4.2) that these operators will not be implicitly instantiated unless you said
"#include <utility>, using namespace std::rel_ops" ?
I finally got it accepted after replacing
"using namespace std" with
[cpp]
using std::cin;
using std::cout;
using std::valarray;
using std::inner_product;
using std::for_each;
using std::fill_n;
[/cpp]
I know that
"using namespace ..." is potentially dangerous, but in this case it shouldn't have caused any problems!
Now I'm getting another weird error with 101 The Blocks Problem, and this trick don't work anymore:
[cpp]
#include <iostream>
#include <vector>
#include <string>
#include <list>
using std::vector;
using std::list;
using std::string;
using std::for_each;
using std::find;
using std::cin;
using std::cout;
using std::back_inserter;
typedef list<int> bstack;
class set_location{
vector<int>& loc;
int b;
public:
set_location(vector<int>& location, int bb) : loc(location), b(bb){}
void operator()(int a){loc[a]=loc[ b ];}
};
class put_block_back{
vector<int>& loc;
vector<bstack*>& st;
public:
put_block_back(vector<bstack*>& stacks, vector<int>& location) :
loc(location), st(stacks){}
void operator()(int a){
loc[a]=a;
st[a]->push_back(a);
}
};
inline void unstack(vector<bstack*>& stacks, vector<int>& location, int elem){
bstack* where = stacks[location[elem]];
bstack::iterator tmp = find(where->begin(), where->end(), elem);
tmp++;
for_each(tmp, where->end(), put_block_back(stacks, location));
where->erase(tmp, where->end());
}
/* ... */
[/cpp]
Online Judge wrote:
Here are the compiler error messages:
01787343_24.c: In function `void unstack(vector<list<int,allocator<int> >
*,allocator<list<int,allocator<int> > *> > &, vector<int,allocator<int> > &, int)':
01787343_24.c:40: implicit declaration of function `int find(...)'
01787343_24.c:40: warning: cannot pass objects of type `_List_iterator<int,int &,int *>' through
`...'
01787343_24.c:40: warning: cannot pass objects of type `_List_iterator<int,int &,int *>' through
`...'
01787343_24.c:40: conversion from `int' to non-scalar type `_List_iterator<int,int &,int *>'
requested
01787343_24.c:42: implicit declaration of function `int for_each(...)'
01787343_24.c:42: warning: cannot pass objects of type `_List_iterator<int,int &,int *>' through
`...'
01787343_24.c:42: warning: cannot pass objects of type `_List_iterator<int,int &,int *>' through
`...'
01787343_24.c: In function `int main()':
01787343_24.c:69: warning: cannot pass objects of type `_List_iterator<int,int &,int *>' through
`...'
01787343_24.c:69: warning: cannot pass objects of type `_List_iterator<int,int &,int *>' through
`...'
01787343_24.c:69: conversion from `int' to non-scalar type `_List_iterator<int,int &,int *>'
requested
01787343_24.c:70: warning: cannot pass objects of type `_List_iterator<int,int &,int *>' through
`...'
01787343_24.c:70: warning: cannot pass objects of type `_List_iterator<int,int &,int *>' through
`...'
--
Can anyone please explain, how can
[cpp]
template <class InputIterator, class T>
InputIterator find(InputIterator first,
InputIterator last,
const T& value);
[/cpp]
return
int???
Thanks...