Please, try EDU on Codeforces! New educational section with videos, subtitles, texts, and problems. ×

CLICKHERE's blog

By CLICKHERE, history, 7 weeks ago, In English,
#include <bits/stdc++.h>
using namespace std;

class a{
public:
	vector<int> arr(5);
};

int main() {
 a b;
}

why it is giving error at vector<int> arr(5);

 
 
 
 
  • Vote: I like it
  • +2
  • Vote: I do not like it

»
7 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

It's not allowed. you can use vector <int> arr = vector<int> (5);

  • »
    »
    7 weeks ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it

    Okay but why? how to declare this in class vector<vector<pair<T, pair<T, T2>>>> adjList(5) ?

    vector<vector<pair<T, pair<T, T2>>>> adjList = vector<vector<pair<T, pair<T, T2>>>> (5) like this?

    • »
      »
      »
      7 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      yes like that. it's a limitation for class member. reason may be it's almost look like a member function vector <int> as a return type arr as a function name.

    • »
      »
      »
      7 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      you can use auto type

      vector<vector<pair<T, pair<T, T2>>>> adjList = vector<vector<pair<T, pair<T, T2>>>> (5)

      this code is equal to fist code:

      auto adjList = vector<vector<pair<T, pair<T, T2>>>> (5)

»
7 weeks ago, # |
  Vote: I like it +1 Vote: I do not like it

Vector size is set in the constructor function.

»
7 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

if you are declaring a class of 'a' then to declare a vector, you should follow this syntax

class fun { vector *v; public: fun() { v = new vector [5]; } };

this syntax is correct...i have used it so many times.

»
7 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

thanks in advance for writing this blog! I have had the same problem for a long time :)