本文介绍了插入和使用自定义类填充Vector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

在我的应用程序中,我有2个类,一个是基类(称为Categorie),另一个是派生类(称为CAT1).我创建了自定义类CAT1的向量,并尝试使用push_back函数用CAT1元素填充该向量:

以下是我的课程的声明:

[基类]

Hi All,

In my application i''ve 2 classes, a Base one (callaed Categorie) and a derived one (called CAT1). I create a vector of my custom class CAT1 and i tried to fill that vector with CAT1 elements using the push_back function:

Below are the declaration of my classes :

[Base Class]

class Categorie
{
    //Attributs
public:
    int ID;
    string Fournisseur;
    int Min;
    int Quantite;
    float Prix_Vente;
    //Constructeurs
public:
    Categorie(int ID, string Fournisseur, int Min, int Quantite, float Prix_Vente)
    {
        this->ID = ID;
        this->Fournisseur = Fournisseur;
        this->Min = Min;
        this->Quantite = Quantite;
        this->Prix_Vente= Prix_Vente;
    }

    Categorie(const Categorie & CSrc)
    {
        this->ID = CSrc.ID;
        this->Fournisseur = CSrc.Fournisseur;
        this->Min = CSrc.Min;
        this->Quantite = CSrc.Quantite;
        this->Prix_Vente= CSrc.Prix_Vente;
    }
};


[结束基类]

[派生类]


[End Base Class]

[Derived Class]

class CAT1: public Categorie
{
    //Attributs
public:
    string Utilite;
    //Constructeurs
    CAT1(int ID, string Fournisseur, int Min, int Quantite, float Prix_Vente, string Utilite):
        Categorie(ID,Fournisseur, Min, Quantite, Prix_Vente), Utilite(Utilite){}

    CAT1(const CAT1 & CSrc) : Categorie(CSrc), Utilite(CSrc.Utilite){}

};



[派生类]

在这里,我写的用CAT1元素填充矢量的行:



[End Derived Class]

And here the lines that i wrote to fill the vector with elements if CAT1:

CAT1 * instance;
instance = AjoutCAT1(); // declared CAT1* AjoutCAT1(){...}
CAT1Vct.push_back(instance);



编译时出现此错误:



When Compiling i got this error :

error C2664: ''push_back'' : cannot convert parameter 1 from ''class CAT1 *'' to ''const class CAT1 &''<br />




有什么问题以及如何解决?

谢谢.




what''s wrong and how fix it ?

Thank you.

推荐答案

std::vector<CAT1> CAT1Vct;



但要使用指针,应该是



but to work with pointers it should be

std::vector<CAT1*> CAT1Vct;



希望这会有所帮助,
弗雷德里克(Fredrik)



Hope this helps,
Fredrik


这篇关于插入和使用自定义类填充Vector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-23 16:16