我想在第3版vc6上編譯該程序。它看到這3個朋友功能無法訪問私人成員。我已經在friend函數聲明中的operator ==之後刪除了<>,因爲它無法通過語法檢查。模板好友功能不能訪問私人會員
這可能是一個衆所周知的問題,我想這可能是由於朋友函數名稱不匹配,但我只是不知道如何糾正它。請幫助我。由於 的代碼如下:
// 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();
}
,因爲我不能添加大量的文字來評論,我貼在VS2008中的一些錯誤。很明顯,以前的錯誤是由於vc6造成的,非常感謝。 完整的代碼在這裏:http://www2.research.att.com/~bs/matrix.c看起來這些語法錯誤與friend函數無關,而是數字中的inner_product。 在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
是否 「VC6」 指的是微軟的Visual C++ 6?該編譯器早於語言標準數年,並且無法編譯大量有效的代碼,特別是如果它使用模板。用本世紀的編譯器你會更好。 –
我在Visual Studio 2008上試過了,出現了很多錯誤。 – shangping
也許你可以發佈這些錯誤?它在GCC上爲我完成編譯,一旦我放回了你刪除的模板說明符。 –