ARRAY[T, SIZE]合适的默认构造函数可用

ARRAY[T, SIZE] Appropriate Default Constructor Available

本文关键字:默认 构造函数 SIZE ARRAY      更新时间:2023-10-16
using namespace std;
template<class T, int SIZE>
class Array
{
private:
    T array[SIZE];
public:
    T& operator[](int nIndex)
    {
        return array[nIndex];
    }
 };
template<class T1, class T2> 
class Pairs
{
private:
    T1 first;
    T2 second; T2 third;
public:
    Pairs(const T1& t1, const T2& t2) : 
        first(t1), second(t2)
    {}
};
int main(int argc, char** argv)
{    
   Array<Pairs<int, int>, 40> sample;
   sample[0] =  Pairs <int, int> (40, 20);
   return 0;
}

我对c++还是个新手。刚刚尝试了一些模板,并得到:

error: ARRAY<T,SIZE>: no appropriate default constructor available.

我该如何解决这个问题?

您的类Pairs没有默认构造函数。Array< Pairs<int, int>, 40>的一个实例需要构造40个Pairs的实例,但是没有构造函数可以这样做。

如果你想让PairsArray中可用,给它一个默认构造函数(一个没有参数的)