模板类无法找到构造函数

时间:2014-11-03 19:23:29

标签: c++ templates

我目前正在研究模板类(我很遗憾)。 我想构建一个可以存储对象的模板类。

这是我的.h文件:

#ifndef STORAGE_H_
#define STORAGE_H_

#include <iostream>
using namespace std;

template<class T,int maxsize>
class Storage
{
    public:
        Storage();

        int getSize();
        T get(int index);
        bool add(T element);

    private:
        T array[maxsize];
        int length;

};

template<class T,int maxsize>
Storage<T,maxsize>::Storage()
  : length(0),// Liste d'initialisation des données membres
{}

template<class T,int maxsize>
int Storage<T,maxsize>::getSize()
{
    return length;
}

template<class T,int maxsize>
T Storage<T,maxsize>::get(int index)
{
    return array[index];
}

template<class T,int maxsize>
bool Storage<T,maxsize>::add(T element)
{
    if (length>=maxsize) return false;
    array[length++]=element;
    return true;
}

#endif /* STORAGE_H_ */

例如,当我处理int时它工作正常。但是当涉及到其他对象时,我之前使它失败了。 在我的主.cpp文件中:

Storage<Pokemon,4> pokemonArray;

它向我发送了以下错误:

no matching function for call to 'Pokemon::Pokemon()'

口袋妖怪是我的一个班级。对我来说,这一行意味着它无法找到构造函数Pokemon()。

我怎样才能让它发挥作用?

谢谢你们!

3 个答案:

答案 0 :(得分:2)

以下代码将起作用,假设Pokemon有一个默认构造函数:

#include <iostream>
using namespace std;

template<class T,int maxsize>
class Storage
{
    public:
        Storage();

        int getSize();
        T get(int index);
        bool add(T element);

    private:
        T array[maxsize];
        int length;

};

template<class T,int maxsize>
Storage<T,maxsize>::Storage()
  : length(0)
{}

template<class T,int maxsize>
int Storage<T,maxsize>::getSize()
{
    return length;
}

template<class T,int maxsize>
T Storage<T,maxsize>::get(int index)
{
    return array[index];
}

template<class T,int maxsize>
bool Storage<T,maxsize>::add(T element)
{
    if (length>=maxsize) return false;
    array[length++]=element;
    return true;
}

class Pokemon{

    public: Pokemon(){};
};

int main(){
    Storage<Pokemon,4> pokemonArray;
}

请注意,如果没有给出构造函数,编译器将自动生成一个,代码仍然可以编译。

class Pokemon{
    //no constructors is still ok
};

默认构造函数由Storage类中的Array隐式调用。你很可能在你的Pokemon类中有一个非平凡的构造函数导致了这个问题。也许是这样的:

class Pokemon{

    public: 
    Pokemon(std::string name){
        //....
    }
    //...
};

当你提供一个非平凡的构造函数时,编译器不会隐式添加一个,因此,你需要更改你的Storage类,或者给你的当前构造函数的Pokemon类一个默认构造函数:< / p>

class Pokemon{

    public: 
    Pokemon(){}; //<-- needed
    Pokemon(std::string name){ ... }; //<-- optional
};

答案 1 :(得分:1)

由于您的Storage模板化类具有固定大小T(即Pokemon个对象)的数组:

template<class T, int maxsize>
class Storage
{
public:
    Storage();

    int getSize();
    T get(int index);
    bool add(T element);

private:
    T array[maxsize]; // Array
    int length;
};

当您尝试实例化该类的对象时,您应该确保Pokemon类具有可见的默认构造函数来初始化该对象数组。以下不会起作用:

class Pokemon {
public:
    Pokemon() = delete;
};

int main(void)
{
    Storage<Pokemon, 4> pokemonArray;

    return 0;
}

解决方案:确保该类具有默认构造函数或change your design(内部存储的智能指针的std::vector怎么办?)

答案 2 :(得分:1)

你的错误很简单:

您正在使用初始化存储空间。

使用未初始化的存储来存储这些成员,例如匿名工会 当您接管了包含的T元素的生命周期管理时,您需要编写自己的copy-ctor,move-ctor,copy-assignment,move-ctor和析构函数。

另一种方法是重新使用已经通过动态存储为您做的标准容器,但看起来您想构建自己的并避免堆。

或者,您知道,通过为其提供默认值,确保您的类型是默认构造的。

相关问题