mercredi 1 novembre 2017

Create a array of shared_ptr with objects

I'm new to using smart pointers, I'm trying to create the same but I have errors.

    class data{
private:
    int ID;
public:
    void setID(int a){
        ID=a;
    }
    int getID(){
        return ID;
    }};

   int main(){
    data d*;
    d=new data[30];
    for(int i=0;i<30;i++){
      (p+i)->setID(i);
   }}

I try with:

shared_ptr<data> sp( new data[30]);
 for(int i=0;i<30;i++){
    sp->setID(i)[i];
}

Error:invalid types 'void[int]' for array subscript|

shared_ptr<data> sp( new data[30]);
for(int i=0;i<30;i++){
    (sp+i)->setID(i);
}

Error: No match for 'operator+' (operand types are 'std::shared_ptr' and 'int')|

how can i do the same?

Aucun commentaire:

Enregistrer un commentaire