How to use vector to store a class?

Asked

Viewed 2,706 times

8

Example:

 class Nome
    {private:
             string nome;
        public:
            Nome(string nome);
            virtual void exibirNome() =0;

    };
class SobreNome: public Nome
    {private:
             string nome;
        public:
            SobreNome(string nome, string sobre_nome): Nome(nome) ;

            void exibirNome();

    };

In the main

vector<Nome> *nome;
 Nome *n  = new SobreNome("João", "Alves");
nome->push_back(n);

'cause I’m not getting it.

error: cannot allocate an Object of Abstract type 'Name"

What’s the right way?

  • Enter the code more fully for us to see.

  • I gave an answer assuming a few things, with the full code I could give an even better answer.

  • @bigown code is just like that

  • I actually had to complete it to get it tested. I made one working, another with the requested adjustment in the reply comment and another using automatic memory management, which is the most correct.

2 answers

6


The code has some problems, I will not fix everything, but I will allow at least it to be compiled:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Nome {
    protected:
        string nome;
    public:
        Nome(string _nome) {
            nome = _nome;
        }
        virtual void exibirNome() = 0;
};
class SobreNome : public Nome {
    string sobre_nome;
    public:
        SobreNome(string nome, string _sobre_nome) : Nome(nome) {
            sobre_nome = _sobre_nome;
        }
        void exibirNome() {
            cout << nome << " " << sobre_nome;
        }
};

int main() {
    vector<Nome*> *nome = new vector<Nome*>;
    Nome *n = new SobreNome("João", "Alves");
    nome->push_back(n);
    nome->at(0)->exibirNome();
    return 0;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

To use the access by the index operator you have to do this:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Nome {
    protected:
        string nome;
    public:
        Nome(string _nome) {
            nome = _nome;
        }
        virtual void exibirNome() = 0;
};
class SobreNome : public Nome {
    string sobre_nome;
    public:
        SobreNome(string nome, string _sobre_nome) : Nome(nome) {
            sobre_nome = _sobre_nome;
        }
        void exibirNome() {
            cout << nome << " " << sobre_nome;
        }
};

int main() {
    vector<Nome*> nome;
    Nome *n = new SobreNome("João", "Alves");
    nome.push_back(n);
    nome[0]->exibirNome();
    return 0;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

It’s not time yet but then try to use smart pointers in place of raw pointers. This code is simple and does no damage, but in real code this would leak memory. C++ is not Java, you have to manage the memory.

#include <iostream>
#include <string>
#include <vector>
#include <memory>
using namespace std;

class Nome {
    protected:
        string nome;
    public:
        Nome(string _nome) {
            nome = _nome;
        }
        virtual void exibirNome() = 0;
};
class SobreNome : public Nome {
    string sobre_nome;
    public:
        SobreNome(string nome, string _sobre_nome) : Nome(nome) {
            sobre_nome = _sobre_nome;
        }
        void exibirNome() {
            cout << nome << " " << sobre_nome;
        }
};

int main() {
    vector<unique_ptr<Nome>> nome;
    nome.emplace_back(new SobreNome("João", "Alves"));
    nome[0]->exibirNome();
    return 0;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • how to call the display method Name ? so it is not working nome[0]->exibirNome();

  • it was really bad, I just didn’t put all the code because I could close without saving, the next times I do right

2

You are assigning the class to your vector pointer, so you will not have an array but an allocated class on the pointer. You must create pointer instance first of all.

class C{
    C(){
        std::cout << "instanciada" << std::endl;
    }
};

using namespace std;

int main(){
    vector<C> class_vec; // cria um vetor de classes C
    C my_class;
    class_vec.push_back(my_class); // adiciona a classe na lista

    my_class = class_vec[0]; // retorna a primeira classe inserida na lista
}

You can also do the process using pointers:

#include <vector>

class C{};

using namespace std;

int main(){
    vector<C*> *class_vec = new vector<C*>(); // cria um vetor de classes C
    C *my_class = new C();
    class_vec->push_back(my_class); // adiciona a classe na lista

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.