vc6의 제 3 판에서 프로그램을 컴파일하려고합니다. 그 3 친구 기능은 개인 회원을 액세스 할 수없는 것으로 나타났습니다. 구문 검사를 통과 할 수 없기 때문에 친구 함수 선언에서 연산자 == 다음에 <>을 제거했습니다.템플릿 친구 함수가 전용 멤버에 액세스 할 수 없습니다.
이것은 잘 알려진 문제 일 수 있습니다. 친구 기능 이름 불일치로 인한 것일 수 있지만 그 문제를 해결하는 방법을 모르겠습니다. 친절하게 도와주세요. 덕분에 코드는 다음과 같습니다 : 나는이 의견에 많은 텍스트를 추가 할 수 없기 때문에
// 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와 관련이 있습니다. 비주얼 스튜디오 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"은 Microsoft Visual C++ 6을 의미합니까? 이 컴파일러는 언어 표준보다 몇 년 앞서서, 특히 템플릿을 사용하는 경우 많은 유효한 코드를 컴파일하지 못합니다. 당신은 금세기의 컴파일러로 훨씬 나아질 것입니다. –
Visual Studio 2008에서 많은 오류가 발생했습니다. – shangping
아마도 이러한 오류를 게시 할 수 있습니까? 내가 제거한 템플릿 지정자를 다시 넣으면 GCC에서 저를 위해 깔끔하게 컴파일됩니다. –