Dragono63
Dragono63

Reputation: 3

Receiving error while calling push_back() method on Vector class object

I am receiving the following error while calling push_back() method on the Vector class object:

error: request for member 'push_back' in 'edges.std::vector<_Tp, _Alloc>::operator[]<int, std::allocator<int> >(((std::vector<int>::size_type)a))', which is of non-class type 'int'

I am trying to create a graph using an adjacency list and it isn't working

#include <bits/stdc++.h>

using namespace std;

/****************************************/
/// INPUT / OUTPUT
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
/****************************************/
/// GLOBAL DECLARATIONS
int n, m;
const int nmax = 100005;
vector <int> edges;
/****************************************/

inline void readInput()
{
    f >> n >> m;
    for(int  i = 1 ; i <= m ; ++ i)
    {
        int a, b;
        f >> a >> b;
        edges[a].push_back(b);
    }
}

int main()
{
    readInput();
    return 0;
}

Sorry for the bad writing this is my first question!

Upvotes: 0

Views: 984

Answers (3)

Klaus
Klaus

Reputation: 25593

 edges[a].push_back(b);

edges[a] gets the a.th element from the vector. As you use a vector<int>, the value you get is an int. And you can't call push_back on an int, as the type int has no member function push_back.

push_back as the name said, pushes a new value at the end of a vector. So you have to use edges.push_back(b).

If your intention is to insert a new value at a given position, you have to use std::vector::insert

Upvotes: 1

Simon Meyer
Simon Meyer

Reputation: 1

By doing like this you execute the push_back method on an int and not on a std::vector. Try it instead:

edges.push_back(b);

Upvotes: 0

Gursewak Singh
Gursewak Singh

Reputation: 822

Try changing the edges[a].push_back(b) to edges.push_back(b).

And as bolov commented, please explain what are you trying to achieve with edges[a].push_back(b). You ought to call push_back() method directly on the Vector class object.

Checkout this answer, it may help you out: https://stackoverflow.com/a/34608598/9339019

Upvotes: 0

Related Questions