Reputation: 1001
I am trying to compile the program on the book 3rd edition on vc6. It sees those 3 friend functions cannot access the private members. I have removed the <> after the operator== in the friend function declaration since it cannot pass the syntax check.
This might be a well-known problem, I guess it might be due to the friend function name mismatch, but I just do not know how to correct it. Kindly help me on this. Thanks The code is below:
// Program to test slices and a simple N*M matrix class
// pp 670-674 and 683-684
// No guarantees offered. Constructive comments to [email protected]
#include<iostream>
#include<valarray>
#include<algorithm>
#include<numeric> // for inner_product
using namespace std;
// forward declarations to allow friend declarations:
template<class T> class Slice_iter;
template<class T> bool operator==(const Slice_iter<T>&, const Slice_iter<T>&);
template<class T> bool operator!=(const Slice_iter<T>&, const Slice_iter<T>&);
template<class T> bool operator< (const Slice_iter<T>&, const Slice_iter<T>&);
template<class T> class Slice_iter {
valarray<T>* v;
slice s;
size_t curr; // index of current element
T& ref(size_t i) const { return (*v)[s.start()+i*s.stride()]; }
public:
Slice_iter(valarray<T>* vv, slice ss) :v(vv), s(ss), curr(0) { }
Slice_iter end() const
{
Slice_iter t = *this;
t.curr = s.size(); // index of last-plus-one element
return t;
}
Slice_iter& operator++() { curr++; return *this; }
Slice_iter operator++(int) { Slice_iter t = *this; curr++; return t; }
T& operator[](size_t i) { return ref(i); } // C style subscript
T& operator()(size_t i) { return ref(i); } // Fortran-style subscript
T& operator*() { return ref(curr); } // current element
friend bool operator==(const Slice_iter<T>& p, const Slice_iter<T>& q);
friend bool operator!=(const Slice_iter<T>& p, const Slice_iter<T>& q);
friend bool operator< (const Slice_iter<T>& p, const Slice_iter<T>& q);
};
template<class T>
bool operator==(const Slice_iter<T>& p, const Slice_iter<T>& q)
{
return p.curr==q.curr && p.s.stride()==q.s.stride() && p.s.start()==q.s.start();
}
template<class T>
bool operator!=(const Slice_iter<T>& p, const Slice_iter<T>& q)
{
return !(p==q);
}
template<class T>
bool operator<(const Slice_iter<T>& p, const Slice_iter<T>& q)
{
return p.curr<q.curr && p.s.stride()==q.s.stride() && p.s.start()==q.s.start();
}
since I cannot add a lot text to comments, I posted some errors in vs2008. It is quite clear that the previous error is due to the vc6, thanks a lot. complete code is at here:http://www2.research.att.com/~bs/matrix.c It seems those syntax error is not related to the friend function, but the inner_product in numeric. some error message under visual studio 2008:
1>c:\vc2008\vc\include\xutility(764) : error C2039: 'iterator_category' : is not a member of 'Cslice_iter<T>'
1> with
1> [
1> T=double
1> ]
1> c:\vc2008\vc\include\numeric(106) : see reference to class template instantiation 'std::iterator_traits<_Iter>' being compiled
1> with
1> [
1> _Iter=Cslice_iter<double>
1> ]
1> k:\c++\valarray0.cpp(245) : see reference to function template instantiation 'double std::inner_product<Cslice_iter<T>,_Ty*,double>(_InIt1,_InIt1,_InIt2,_Ty)' being compiled
1> with
1> [
1> T=double,
1> _Ty=double,
1> _InIt1=Cslice_iter<double>,
1> _InIt2=double *
1> ]
1>c:\vc2008\vc\include\xutility(764) : error C2146: syntax error : missing ';' before identifier 'iterator_category'
1>c:\vc2008\vc\include\xutility(764) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\vc2008\vc\include\xutility(764) : error C2602: 'std::iterator_traits<_Iter>::iterator_category' is not a member of a base class of 'std::iterator_traits<_Iter>'
1> with
Upvotes: 2
Views: 1560
Reputation: 408
You have to specify the type parameter while declaring as friend inside the class definition.
Also, you don't need to specify Slice_iter<T>
since Slice_Iter
inside the class scope implicitly has the type parameters.
friend bool operator==<T>(const Slice_iter& p, const Slice_iter& q);
friend bool operator!=<T>(const Slice_iter& p, const Slice_iter& q);
friend bool operator< <T>(const Slice_iter& p, const Slice_iter& q);
Upvotes: 4