Class c ++ has no member named

0

I'm starting to develop a list of lists to represent a graph in memory.

I just did this first part and am getting the following error:

class std::vector<No> has no member named 'getId'

Follow my code so far ...

main.cpp:

#include <iostream>

using namespace std;

int main()
{
    return 0;
}

no.h:

#ifndef NO_H_INCLUDED
#define NO_H_INCLUDED

#include <vector>
#include "Aresta.h"

using namespace std;

class No{
    private:
        int id;
        vector<Aresta> *adjNo;
    public:
        No(int id);
        int getId();
        vector<Aresta>* getAdjNo();
        void inserirAdj(Aresta aresta);
        int totalAdjacencias();
};

#endif // NO_H_INCLUDED

no.cpp:

#include "No.h"
#include <vector>

No::No(int id){
    this->id=id;
    this->adjNo = new vector<Aresta>;
}

int No::getId(){
    return this->id;
}

vector<Aresta>* No::getAdjNo(){
    return this->adjNo;
}

void No::inserirAdj(Aresta aresta){
    adjNo->push_back(aresta);
}

int No::totalAdjacencias(){
    this->adjNo->size();
}

Edge.h:

#ifndef ARESTA_H_INCLUDED
#define ARESTA_H_INCLUDED

class Aresta{
    private:
        int id;
        int peso;

    public:
        Aresta(int id, int peso);
        int getId();
};

#endif // ARESTA_H_INCLUDED

aresta.cpp:

#include "Aresta.h"

Aresta::Aresta(int id, int peso){
    this->id=id;
    this->peso=peso;
}

int Aresta::getId(){
    return this->id;
}

Graph.h:

#ifndef GRAFO_H_INCLUDED
#define GRAFO_H_INCLUDED

#include "No.h"

using namespace std;

class Grafo{
    private:
        vector<No> *listNos;
    public:
        Grafo();
        void addNo(int id);
        bool existeNo(int id);
};

#endif // GRAFO_H_INCLUDED

grafo.cpp:

#include "Grafo.h"
#include <iostream>

Grafo::Grafo(){
    listNos = new vector<No>;
}

void Grafo::addNo(int id){
    if(!existeNo(id)){
        No noAux(id);
        listNos->push_back(noAux);
    } else
        cout << "No ja existe no Grafo";
}

bool Grafo::existeNo(int id){
    for(int i=0; i < listNos->size(); i++){
        if(listNos[i].getId() == id)
            return true;
    }
    return false;
}
    
asked by anonymous 29.04.2016 / 19:51

1 answer

2

Well, you did not answer my questions, but considering the error message ("class std :: vector has no member named 'getId'"), the error almost certainly occurs on the line containing this code call of the Grafo::existeNo() function):

. . .
if(listNos[i].getId() == id)
. . .

By checking the definition of listNos[i] , it is noted that it is declared as:

. . .
vector<No> *listNos;
. . .

That is, you are declaring a pointer to a vector from the default library (that is, std::vector ), which contains static instances of class No . Thus, the error is due to the fact that listNos is a pointer, not a statically allocated class.

Consider this didactic example:

#include <vector>
#include <iostream>

using namespace std;

int main()
{
    // Este é um vetor de inteiros, alocado estaticamente
    vector<int> vPares;

    // Este é um vetor de inteiros, alocado dinâmicamente
    vector<int> *vImpares;
    vImpares = new vector<int>();

    // Insere números nos vetores
    for (int i = 0; i <= 20; i++)
    {
        if(!(i % 2))
            vPares.push_back(i);
        else
            vImpares->push_back(i);
    }

    // Imprime o conteúdo dos vetores:
    cout << "Numeros pares: ";
    for (unsigned int i = 0; i < vPares.size(); i++)
    {
        cout << vPares[i] << " ";
    }
    cout << endl;

    cout << "Numeros impares: ";
    for (unsigned int i = 0; i < vImpares->size(); i++)
    {
        cout << (*vImpares)[i] << " ";
        //cout << vImpares->at(i) << " "; // <= ALTERNATIVA SEM PRECISAR DO *
    }
    cout << endl;

    // Como vImpares foi alocado dinâmicamente, precisa ser desalocado!
    delete vImpares;

    return 0;
}

Whose output is this:

Numeros pares: 0 2 4 6 8 10 12 14 16 18 20
Numeros impares: 1 3 5 7 9 11 13 15 17 19

You will realize that if you allocate dynamically using a pointer, you need to reference it as a pointer (by adding * ) before to use normally. In your case, just use:

(*listNos[i]).getId()

or alternatively (as in the previous didactic example):

listNos->at(i).getId()

Just to complete the information, if you use an iterator (iterator ), it might be easier. In my example:

. . .
cout << "Numeros impares: ";
for (vector<int>::iterator it = vImpares->begin(); it != vImpares->end(); ++it)
{
    cout << *it << " ";
}
cout << endl;
. . .

In your case:

. . .
for (vector<No>::iterator no = listNos->begin(); no != listNos->end(); ++no)
{
    no->getId()
. . .
    
30.04.2016 / 00:55