Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.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
.net 托管类类型作为类型参数T的类型(错误C2670)_.net_Generics_Visual C++_C++ Cli_Generic Collections - Fatal编程技术网

.net 托管类类型作为类型参数T的类型(错误C2670)

.net 托管类类型作为类型参数T的类型(错误C2670),.net,generics,visual-c++,c++-cli,generic-collections,.net,Generics,Visual C++,C++ Cli,Generic Collections,我想创建一个泛型函数,返回类型为observetecollection^的集合。调用者为类型参数T传递托管类类型。 下面是我对泛型函数的一次尝试,之后是对所用其他类的解释: generic<class T> where T:CAnimal, gcnew() ObservableCollection<T>^ GetItems(AnimalType animalType, int count) { ObservableColle

我想创建一个泛型函数,返回类型为
observetecollection^
的集合。调用者为类型参数
T
传递托管类类型。 下面是我对泛型函数的一次尝试,之后是对所用其他类的解释:

generic<class T>
    where T:CAnimal, gcnew()
        ObservableCollection<T>^ GetItems(AnimalType animalType, int count)
    {
        ObservableCollection<T>^ animals = gcnew ObservableCollection<T>();
        for (int i = 0; i < count; i++)
        {
            if (animalType == Dog)
            {
                CDog^ dog = gcnew CDog(i, count - i);
                //CAnimal^ dog = gcnew CDog(i, count - i);
                //T dog = gcnew CDog(i, count - i);
                //T^ dog = gcnew CDog(i, count - i);

                animals->Add(dog);
            }
            //else if (animalType == Cat) { ... }
            //...
        }

        return animals;
    }
此外,还有一个父类持有狗、猫等的
可观察集合

ref class CZoo
{
private:
    ObservableCollection<CDog^>^ dogs;

public:
    CZoo()
    {
        dogs = GetItems<CDog^>(AnimalType::Dog, 3);
    }
};

如果我删除了与错误无关的内容,我们得到的是:

generic<class T>
where T:CAnimal, gcnew()
ObservableCollection<T>^ GetItems()
{
    ObservableCollection<T>^ animals = gcnew ObservableCollection<T>();

    animals->Add(gcnew CDog());

    return animals;
}
  • 因为您得到了
    gcnew()
    约束,这意味着该类有一个零参数构造函数,您可以使用type
    T
    调用它
  • 将当前拥有的构造函数参数移动到
    CAnimal
    上定义的方法中。这样,您可以在任何
    T
    上调用它
  • 我删除了enum参数。你不需要它;您已经通过通用程序指定了所需的动物。
    • 我建议您进行此更改,无论您如何实施此更改。否则,你必须考虑什么东西,比如“代码> GETIETEMS(AnimalType::猫,3)< /COD>应该做。

感谢您的解释和解决我问题的各种方法。我将最后的代码整合到问题中。对您建议的泛型函数所做的唯一更改是将
T^animal=…
更改为
T animal=…
,因为不允许对泛型类型参数进行独立(错误C3229)。我在键入时没有使用编译器,感谢您的更正。修正了。谢谢你在没有留下评论的情况下投了反对票。真有趣!
ref class CAnimal
{
internal:
    virtual void Initialize(int _age, int _other)
    {
        age = _age;
        other = _other;
    }

public:
    int age;
    int other;
};

ref class CDog : CAnimal {};
ref class CCat : CAnimal {};

generic<class T>
    where T:CAnimal, gcnew()
        ObservableCollection<T>^ GetItems(int count)
    {
        ObservableCollection<T>^ animals = gcnew ObservableCollection<T>();
        for (int i = 0; i < count; i++)
        {
            T animal = gcnew T();
            animal->Initialize(i, count - i);
            animals->Add(animal);
        }

        return animals;
    }

ref class CZoo
{
private:
    ObservableCollection<CDog^>^ dogs;
    ObservableCollection<CCat^>^ cats;

public:
    CZoo()
    {
        dogs = GetItems<CDog^>(3);
        cats = GetItems<CCat^>(7);
    }
};
generic<class T>
where T:CAnimal, gcnew()
ObservableCollection<T>^ GetItems()
{
    ObservableCollection<T>^ animals = gcnew ObservableCollection<T>();

    animals->Add(gcnew CDog());

    return animals;
}
generic<class T>
where T:CAnimal, gcnew()
ObservableCollection<T>^ GetItems(int count)
{
    ObservableCollection<T>^ animals = gcnew ObservableCollection<T>();

    for (int i = 0; i < count; i++)
    {
        T animal = gcnew T();
        animal->Initialize(i, count-i);
        animals->Add(animal);
    }

    return animals;
}