Class c++ has no Member named

Asked

Viewed 539 times

0

I’m starting to develop a list list to represent a graph in memory.

I’ve only done that first part and I’m getting the following mistake:

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.:

#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

in 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

edge.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

cpp graph.:

#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;
}
  • 1

    On which line of the code the error occurs?

  • It’s on the line that you have if(listNos[i].getId() == id)? Why listNos is a pointer to a vector?

1 answer

2


Well, you didn’t 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 (within the function Grafo::existeNo()):

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

By checking the definition of listNos[i], it is observed that he is declared so:

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

That is, you are declaring a pointer to a vector of the standard library (i.e., a std::vector), containing static instances of the 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;
}

That’s the way out:

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 notice that if you allocate dynamically using a pointer, you need to reference it as a pointer (by adding the *) before to use normally. In your case, just use:

(*listNos[i]).getId()

or alternatively (as in the previous example):

listNos->at(i).getId()

Just to complete the information, if you use an iterator (iterator), may 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()
. . .
  • Extremely perfect his reply @Luiz Vieira, I could not answer your questions before because I was traveling. One more thing, in the context of your teaching example, how do I remove item 7 for example using pointers...

  • Thank you Dudu. You do the removal normally. Having a pointer to an array only makes you need to access its methods/attributes through the operator -> instead of the operator .. To remove by index use std::vector::erase() and to remove by value use also std::remove().

  • So if I do Std::remove(no->getId()) will remove the element that has no with that id passed as parameter?

  • No. As the documentation on the link I gave you indicates (did you read it? ), you need to pass the initial and final iterator as well (something like std::remove(listNos->begin(), listNos->end(), no);. But, what your vector stores are not numerical ids, but static instances of No. Therefore, you need to pass the node iterator, not its id. To work the way you want, you need to implement a comparison operator that, yes, will compare by ID. I suggest you open a new question. This site is not a forum. :)

Browser other questions tagged

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