0

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!

1
  • please remove all the code that has nothing to do with your question and create a proper minimal reproducible example. Then explain what you are trying to achieve and what do you expect edges[a].push_back(b); to do. Commented Sep 27, 2020 at 13:47

3 Answers 3

1
 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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.