我收到以下错误:
Database.hpp: In member function ‘Table<T, S>& Table<T, S>::operator=(const Table<T, S>&) [with T = int, int S = 3, Table<T, S> = Table<int, 3>]’:
Database.hpp:40:8: instantiated from ‘void std::vector<_Tp, _Alloc>::_M_insert_aux(std::vector<_Tp, _Alloc>::iterator, const _Tp&) [with _Tp = List, _Alloc = std::allocator<List>, std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<List*, std::vector<List> >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer = List*]’
/usr/include/c++/4.6/bits/stl_vector.h:834:4: instantiated from ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = List, _Alloc = std::allocator<List>, std::vector<_Tp, _Alloc>::value_type = List]’
SelectiveReading.hpp:139:34: instantiated from here
Database.hpp:34:16: error: invalid initialization of non-const reference of type ‘Table<int, 3>&’ from an rvalue of type ‘Table<int, 3>* const’
我有以下ADT。我不明白它们有什么问题。
// this is Database.hpp
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;
}
};
typedef Table<int,3> Date;
struct List
{
Date AnnouncementDate;
int BonusShares;
int StockSplit;
int CashDividend;
bool DividendPayment;
Date ExDividendDate;
Date ShareRecordDate;
Date BonusSharesListingDate;
};
typedef std::vector<List> Database;
虽然电话是这样的:
List record;
// (...)
Data.push_back(record); // this is SelectiveReading.hpp:139:34
答案 0 :(得分:6)
在operator=
的最后一行,当您打算返回this
(参考)时,您将返回*this
(指针)。
答案 1 :(得分:5)
return this;
必须为return *this;