0

C++ (STL):

Request for member ‘push_back’ in ‘pos.std::vector<int>::operator[](((std::vector<int>::size_type)i))’, which is of non-class type ‘__gnu_cxx::__alloc_traits<std::allocator<int> >::value_type {aka int}’
   pos[i].push_back(tmp);
vector<int> pos(MAX), vector<int> tmp;

I can't figure out why my code is getting wrong. So, Please tell, why is this error coming

Goal: I can't give the whole code as i faced the problem now only while coding a solution to a part of a code in a running contest. So, Please just tell me about this error. Then, my all other parts of code will work fine.

MY CODE:

/*
 Name: SUSHANT OBEROI
 College: MNNIT ALLAHABAD
 Email: sushantoberoi3@gmail.com
 Handle: soc3
*/

#include<bits/stdc++.h>
using namespace std;

#define sd(a) scanf("%d", &a)
#define slld(a) scanf("%lld", &a)
#define fl(i, a, b) for(int i=a; i<b; i++)
#define fle(i, a, b) for(int i=a; i<=b; i++)
#define ll long long
#define wl(q) while(q--)
#define MAX 300005
#define mp make_pair
#define fi first
#define se second
#define mod 1000000007

void print_output() {                                                                               
    vector<int> pos(MAX);
    fl(i, 0, 100) {
        pos[i].clear();
        vector<int> tmp;
        fl(j, 0, 100) 
            tmp.push_back(j);
        pos[i].push_back(tmp);
    }
}

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

I think (according to me only, i don't what's the correct answer is?) that i am doing it correctly but compiler is wrong. (Sorry for saying this, but i think).

JVApen
  • 11,008
  • 5
  • 31
  • 67

1 Answers1

0

You've defined vector<int> pos(MAX); which is a vector of int. The expression pos[i] returns a reference to an element of the vector, which in this case is an int. The statement pos[i].push_back(tmp); tries to call a push_back() member function on an int, which is not a class and does not have that member function.

Perhaps you meant to create a vector<vector<int>> pos(MAX);, a vector of vectors. I don't know and after seeing #include<bits/stdc++.h> and those #define lines I'm not interested in the rest of the code.

Blastfurnace
  • 18,411
  • 56
  • 55
  • 70
  • Then also the same error is coming. Please check yourself as well on linux terminal. – sushant oberoi Jun 01 '19 at 12:16
  • @sushantoberoi If you want to assign `tmp` to `pos[i]` you can just do `pos[i] = tmp;`. It looks like you don't want `pos` to grow (using `push_back`) since you've created it with a given size so just try assignment. Again, I'm just guessing what you're attempting. – Blastfurnace Jun 01 '19 at 12:25
  • Thanks Sir, it's not like that i don't want to grow pos, we can do that, and now i can successfully submit the solution for the contest. But, i usually used pos[i].push_back(tmp), so i coded it up like that. Please, just tell one more thing, how to remove this error if i want to use push_back() without limting the size. – sushant oberoi Jun 01 '19 at 12:30
  • @sushantoberoi You could define `vector> pos;`, note this creates an empty vector, and then do `pos.push_back(tmp);` to append `tmp` to the end of `pos`. See it [here on Compiler Explorer](https://godbolt.org/z/2g8NPX). – Blastfurnace Jun 01 '19 at 12:34
  • If i define it like that, Please tell why is it wrong? – sushant oberoi Jun 01 '19 at 12:37
  • I am saying, I have not written any code which violated C++ syntax. I want to ask, then, why compiler is giving error? – sushant oberoi Jun 01 '19 at 12:44
  • @sushantoberoi Because `vector` is a vector of `int` (a one-dimensional dynamic array of `int`). This means `pos[i]` is an `int`, not a vector of `int`. Attempting to call `push_back()` on an `int` is a syntax error. That's like trying to do `int meow; meow.push_back(42);` which is not valid code. – Blastfurnace Jun 01 '19 at 12:47
  • https://stackoverflow.com/questions/6296945/size-vs-capacity-of-a-vector Sir, just refer to this article once to differentiate between (MAX) and [MAX] and please tell me also if I am wrong? – sushant oberoi Jun 01 '19 at 14:12
  • @sushantoberoi `vector pos(MAX);` (with parentheses) defines a vector with a **size()** of MAX elements. Just like if you had used `pos.resize(MAX)` to set the size. `vector pos[MAX];` (with square brackets) defines an **array** of vectors. This is an array of size MAX, each element is an empty vector. – Blastfurnace Jun 01 '19 at 14:25
  • Sir initially you suggested vector> pos so i thought now also you saying like this vector> pos that's why I couldn't figure out this thing at that time. But now I understood that you are suggesting vector pos[MAX]. Thanks for the answer and investing your precious time. – sushant oberoi Jun 01 '19 at 17:33