簡體   English   中英

模板好友功能無法訪問私有成員

[英]template friend function cannot access private member

我正在嘗試在vc6的第三版上編譯程序。 看到這3個朋友功能無法訪問私有成員。 我在朋友函數聲明中的operator ==之后刪除了<>,因為它無法通過語法檢查。

這可能是一個眾所周知的問題,我想可能是由於朋友函數名稱不匹配所致,但是我只是不知道如何更正它。 請幫助我。 謝謝代碼如下:

   // Program to test slices and a simple N*M matrix class

// pp 670-674 and 683-684

// No guarantees offered. Constructive comments to bs@research.att.com


#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看來那些語法錯誤與朋友功能無關,而與數字的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

在類定義內聲明為好友時,必須指定type參數。

另外,由於類范圍內的Slice_Iter隱式具有類型參數,因此無需指定Slice_iter<T>

 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM