繁体   English   中英

错误:聚集的“ food_type a”类型不完整,无法定义

[英]error: aggregate ‘food_type a’ has incomplete type and cannot be defined

编译器不允许我运行以下代码:

错误:聚集的“ food_type a”类型不完整,无法定义

除非我在实例化af1f2f3之后添加括号() ,否则它将阻止cout函数显示任何内容。 为什么会发生?

#include <iostream>

enum class Animal { Cat, Dog, Duck};

class Food
{
public:
    Food()
    {
        std::cout<<"Food called"<<std::endl;
    }
};

template <enum Animal,class food_type>
class Ecosystem;

template<>
class Ecosystem<Animal::Cat,class food_type>
{
public:
    Ecosystem()
    {
        std::cout<<"Cat constructor called"<<std::endl;
        food_type a;
    }
};

template <>
class Ecosystem<Animal::Dog,class food_type>
{
public:
    Ecosystem()
    {
        std::cout<<"Dog constructor called"<<std::endl;
        food_type a;
    }
};


template <>
class Ecosystem<Animal::Duck,class food_type>
{
public:
    Ecosystem()
    {
        std::cout<<"Duck constructor called"<<std::endl;
        food_type a;
    }
};

int main()
{
    Ecosystem<Animal::Cat,Food> f1;
    Ecosystem<Animal::Dog,Food> f2;
    Ecosystem<Animal::Duck,Food> f3;
    return 0;
}

如果要部分专门化模板,则应按以下步骤进行:

template<class food_type>
class Ecosystem<Animal::Cat,food_type>

代替:

template<>
class Ecosystem<Animal::Cat,class food_type>

在第二种情况下,您实际上在做的是完全基于不完整的类型class food_type ,这是导致错误的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM