-2

I have added bits/stdc+.h and vector both. Still this error is coming . Can anyone tell me why this is happening.

#include <bits/stdc++.h>
#include<vector>
void rotate(int arr[], int n);

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        int a[n] , i;
        for(i=0;i<n;i++)
        scanf("%d",&a[i]);
        rotate(a, n);
        for (i = 0; i < n; i++)
            printf("%d ", a[i]);
        printf("\n");
    }
        return 0;
}
// } Driver Code Ends


//User function Template for C++

void rotate(int arr[], int n)
{
    vector<int> a;
    a[0] = arr[n-1];
    for(int i = 0 ; i<n-1 ;i++)
      {
          a.insert(a.back(), arr[i]);
      }
      
   for(int j : a)
    cout<<j;
}
main.cpp:30:5: error: ‘vector’ was not declared in this scope
     vector<int> a;
     ^~~~~~
JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • 11
    Try changing it to `std::vector` – chessofnerd Jun 26 '21 at 18:49
  • What version of gcc are you using? (I'll assume that is your compiler since the non-standard `bits/stdc++.h` did not kill the compilation.) It looks like versions 4.6 and up will tell you how to fix this via their "suggested alternative" note after the "not declared in this scope" error message. – JaMiT Jun 26 '21 at 19:10
  • 2
    Requests for debugging help should have the *shortest* code necessary to reproduce the error (a.k.a. a [mre]). For compilation errors, it's often useful to try to reduce your code to just what is needed for the line on which the error is detected. In your case, that would be `#include int main() { vector a; }`. This much-simpler code reproduces your error, does it not? – JaMiT Jun 26 '21 at 19:14
  • https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h – SuperStormer Jun 26 '21 at 19:17
  • 2
    We need a `not-really-c++` tag, so I can filter out these questions. – Eljay Jun 26 '21 at 19:19
  • 1
    @Eljay what about this question is not really C++? So yeah it's got a few `scanf` and `printf` but that's not a crime. – Mark Ransom Jun 26 '21 at 19:24
  • Any introductory C++ book should cover this. – n. m. could be an AI Jun 26 '21 at 19:34
  • @MarkRansom `#include ` is not really C++. `scanf("%d",&n);int a[n]` is not really C++. – n. m. could be an AI Jun 26 '21 at 19:36
  • @Eljay perhaps `competitive-programming`? (also does not exist but probably should) – n. m. could be an AI Jun 26 '21 at 19:38
  • [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) [Why is `using namespace std;` considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Evg Jun 26 '21 at 20:25

1 Answers1

-3

Follow these: (EDITED)

  • (SOLUTION TO YOUR PROBLEM) Use, using namespace std as it means if the compiler finds something that is not declared in the current scope then it will go and check std.
  • Don't mix c and c++ syntax. Either use printf or cout.

Also check the 1st comment on this answer, as there is something you should know about "using namespace std" and "cout/cin".

  • No need to work two times, you can also declare and define your function at once.

Solution (but have an error in other parts)

#include <bits/stdc++.h>
using namespace std;
void rotate(int arr[], int n)
{
    vector<int> a;
    a[0] = arr[n - 1];
    for (int i = 0 ; i < n - 1 ; i++)
    {
        a.insert(a.back(), arr[i]); // ITS YOUR SYNTAX, CONSIDER TO UPDATE IT
    }
    for (auto &it : a)
        cout << it;
}
int main()
{
    int t;
    cin>>t;
    while (t--)
    {
        int n;
        cin>>t;
        int a[n] , i;
        for (i = 0; i < n; i++)
            cin>>a[i];
        rotate(a, n);
        for (i = 0; i < n; i++)
            cout<<a[i];
        cout<<"\n";
    }
    return 0;
}

CHECK LINE NO 9 line,

and see if it's correct or not. a.insert(a.back(), arr[i]); WRONG You are doing something wrong there. Check this statement

error: ‘vector’ was not declared in this scope

Solved by using namespace

If you liked this answer. Please consider ticking this answer.:)

  • Do **not** follow these tips. [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) [Why is `using namespace std;` considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Evg Jun 26 '21 at 20:25
  • @Evg Hey! thank you so much for these links, they are really useful. I had just tried to resolve his issue, so that's why I had recommended ** using namespace library **. But anyway thanks for the links. I have also updated the top heading of my answer. :) – HARDIK KAUSHIK Jun 27 '21 at 17:29