samedi 18 juillet 2020

error: expected unqualified-id before 'return'

I was writing a program for array rotation when i encountered this error:-- (The error is showing at the end of the complete code!!)

|51|error: expected unqualified-id before 'return'|
|52|error: expected declaration before '}' token|

Here's my code:--

#include <iostream>
#include "vector"
using std::vector;
using namespace std;

void reverse_array(int arr[], int start,int end1)
{
    while (start < end1) {
        int temp=arr[start];
        arr[start] = arr[end1];
        arr[end1] = temp;
        start++;
        end1--;
    }

}
void rotate_array(int arr[],int N,int D)
{
    if (D == 0)
        return;
    reverse_array(arr,0,D-1);
    reverse_array(arr,D,N-1);
    reverse_array(arr,0,N-1);
}
int main()
{
    int N,D;
    cout<<"Enter the number of elements in array:";
    cin>>N;
    cout<<"By how much do you want to rotate the array:";
    cin>>D;
    int elements;
    int arr[N];
    cout<<"Enter the elements:";
    for (int i = 0;i<=N;i++){
        cin>>elements;
        arr[i] = elements;
    }
    rotate_array(arr,N,D);
    for (int z = 0; z<=N;z++)
        cout<<arr[z];
    return 0;
}

I'm not getting what is actually wrong! I looked at other solutions(error : expected unqualified-id before return in c++)and they mentioned some braces related problems but I can't find any such in my program!

This is what the build log tab shows:

-------------- Build: Debug in Array_Rotation_Reversal_Algorithm (compiler: GNU GCC Compiler)---------------

g++.exe -Wall -fexceptions -g  -c C:\Users\aakash\Desktop\C++\Array_Rotation_Reversal_Algorithm\main.cpp -o obj\Debug\main.o
g++.exe  -o bin\Debug\Array_Rotation_Reversal_Algorithm.exe obj\Debug\main.o   
C:\Users\aakash\Desktop\C++\Array_Rotation_Reversal_Algorithm\main.cpp:45:5: error: expected unqualified-id before 'return'
     return 0;
     ^~~~~~
C:\Users\aakash\Desktop\C++\Array_Rotation_Reversal_Algorithm\main.cpp:46:1: error: expected declaration before '}' token
 }
 ^
Process terminated with status 1 (0 minute(s), 1 second(s))
2 error(s), 0 warning(s) (0 minute(s), 1 second(s))

Edit: It is solved now!

Aucun commentaire:

Enregistrer un commentaire