Alex
Alex

Reputation: 58

Error: expected primary-expression before ']' token in C++

I'm trying to create something similar to string.find with vectors c++98, but I can't fix this error. I have simplified the code and left only the error. If I can get help on how to achieve this.

#include <iostream>
#define MAX 99999
using namespace std;

int free_pos (int v[]);
int find_line (int v[], int pos);

int main() {
   char text[MAX];
   int loc_key[MAX],start_line[MAX]; //

   for(int i=0; i<free_pos(loc_key);i++) {
       cout << "Line: " << i+1;
       int pos = find_line(start_line[],loc_key[i]); //Here is the error
       for(int j=start_line[pos];j<start_line[pos+1];j++) {
            cout<<text[j];
       }
       cout<<endl;
   }
   return 0;
}

int free_pos (int v[]) {
    for(int i=0;i<MAX;i++){
        if(v[i] == 0)
            return i;
    }
    return 99;
}

int find_line (int v[], int pos) {
    for(int i=0; i<free_pos(v); i++) {
        if(v[i]==pos)
            return v[i];
        if(v[i]< pos)
            return v[i-1];
    }
}

Upvotes: 2

Views: 8318

Answers (2)

xp4
xp4

Reputation: 1

#include<bits/stdc++.h>
using namespace std;
int max(int arr[],int n){
maximum=INT_MAX;
for(int i=0;i<n;i++){
if(arr[i]>maximum){
maximum=arr[i]}}
return maximum;}

int main(){
int size;
cin>>size;
for(int i=0;i<size;i++){
cin>>arr[i]}
cout<<max(arr,size);
return 0;}

Upvotes: 0

whiskeyo
whiskeyo

Reputation: 914

Your both functions

int free_pos (int v[]);
int find_line (int v[], int pos);

take arrays as an input, but then you try to call int pos = find_line(start_line[],loc_key[i]);, which should take the name of an array only, as it is the name which is known by the program as an array. You can easier (in my opinion) get it, if you write both of these functions following way:

int free_pos (int* v);
int find_line (int* v, int pos);

They do the same job as your functions, but you can see that their arguments are: an int pointer (array) and an int. Then, calling the function needs only the int pointer, which in your case is start_line, and the integer loc_key[i]. If you still have problems understanding it, you could read about dynamic memory allocation of arrays, which is done as: int* arr; arr = new int[your_size]; - I think it is clearer while thinking that way.

Upvotes: 2

Related Questions