Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ &引用;无效使用非静态数据成员“;访问模板类时';字段通过友好输出运算符_C++_Templates_Friend - Fatal编程技术网

C++ &引用;无效使用非静态数据成员“;访问模板类时';字段通过友好输出运算符

C++ &引用;无效使用非静态数据成员“;访问模板类时';字段通过友好输出运算符,c++,templates,friend,C++,Templates,Friend,当我试图通过befriended output操作符访问模板化类的字段时,出现以下错误 Database.hpp: In function ‘std::ostream& ostream(std::ostream&, const Table<T, S>&)’: Database.hpp:10:12: error: invalid use of non-static data member ‘Table<T, S>::tab’ Database.hpp

当我试图通过befriended output操作符访问模板化类的字段时,出现以下错误

Database.hpp: In function ‘std::ostream& ostream(std::ostream&, const Table<T, S>&)’:
Database.hpp:10:12: error: invalid use of non-static data member ‘Table<T, S>::tab’
Database.hpp:41:19: error: from this location
Database.hpp: In function ‘std::ostream& operator<<(std::ostream&, const List&)’:
Database.hpp:62:17: error: no match for ‘operator<<’ in ‘os << l.List::AnnouncementDate’
Database.hpp:在函数“std::ostream&ostream(std::ostream&,const Table&)”中:
Database.hpp:10:12:错误:非静态数据成员“Table::tab”的使用无效
Database.hpp:41:19:错误:来自此位置

Database.hpp:在函数'std::ostream&operator中,看起来您想命名朋友函数
operator
t.tab[i++]
?友元函数在下一个非类名称空间中声明,即:global namespace.Oops,你犯了一个愚蠢的错误!天哪,犯了这么愚蠢的错误,我感到羞愧。谢谢你的帮助:)
template <class T, int S>
class Table
{
    T tab[S];
    public:
    inline T& operator[](int i)
    {
        return tab[i];
    }
    inline const T& operator[](int i) const
    {
        return tab[i];
    }
    Table() {}
    ~Table() {}
    Table(const Table &t)
    {
        for ( int i=0; i<S; ++i )
        {
            tab[i] = t[i];
        }
    }
    Table& operator=(const Table &t)
    {
        for ( int i=0; i<S; ++i )
        {
            tab[i] = t[i];
        }
        return *this;
    }
    friend std::ostream& ostream( std::ostream& os, const Table<T,S> &t )
    {
        for ( int i=0; i<3; os << '-' )
        {
            os << tab[i++];
        }
        return os;
    }
};

typedef Table<int,3> Date;

struct List
{
    Date AnnouncementDate;
    int BonusShares;
    int StockSplit;
    int CashDividend;
    bool DividendPayment;
    Date ExDividendDate;
    Date ShareRecordDate;
    Date BonusSharesListingDate;

    friend std::ostream& operator<<( std::ostream& os, const List &l )
    {
        os << l.AnnouncementDate << ',' << std::endl <<
        l.BonusShares << ',' << std::endl <<
        l.StockSplit << ',' << std::endl <<
        l.CashDividend << ',' << std::endl <<
        l.DividendPayment << ',' << std::endl <<
        l.ExDividendData << ',' << std::endl <<
        l.ShareRecordDate << ',' << std::endl <<
        l.BonusSharesListingDate << ',' << std::endl;
        return os;
    }
};

typedef std::vector<List> Database;
friend std::ostream& operator<<( std::ostream& os, const Table<T,S> &t )
//                   ^^^^^^^^^^
os << t.tab[i++];