Need help with Arrays

Hi I'm new to C++ and have a quick question about arrays. Why doesn't this function like my array?

I keep getting a "invalid types 'const int[int]' for array subscript" error and "expected primary-expression before ']' token"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

#include "Header.h"

int main ()
{

	const int AR_LIST[]       = {7, 12, 9, 18,};
	const int AR_SIZE         = 4;
	const string NAME_ARRAY[] = {"Jill", "Bob", "Greg", "Steven"};


	//INPUT - Function that asks the user for input(see GetSalesInfo.cpp)

	FindFirstNum(AR_LIST[], AR_SIZE, NAME_ARRAY[]);

	LowestValArray(AR_LIST[], AR_SIZE, NAME_ARRAY[]);


	return 0;
}

and this is my header file:

#ifndef HEADER_H_
#define HEADER_H_

//Pre-Processor Directives
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <time.h>

using namespace std;

void FindFirstNum(const int AR_LISTF, const int AR_SIZEF, const string NAME_ARRAYF);

int LowestValArray(const int AR_LISTF, const int AR_SIZEF, const string NAME_ARRAYF);

#endif /* HEADER_H_ */ 
1
2
FindFirstNum(AR_LIST[], AR_SIZE, NAME_ARRAY[]);
LowestValArray(AR_LIST[], AR_SIZE, NAME_ARRAY[]);

You can just write AR_LIST and NAME_ARRAY without the square brackets here. Use the subscript operator (suffice to say that's the fancy name for the square brackets) when you want to access a particular item in an array. ie AR_LIST is the whole array (it's not technically, but semantically you can think of it as the whole array) AR_LIST[5] is the sixth element (0 is the first) in the array.

1
2
void FindFirstNum(const int AR_LISTF, const int AR_SIZEF, const string NAME_ARRAYF);
int LowestValArray(const int AR_LISTF, const int AR_SIZEF, const string NAME_ARRAYF);

These should take pointers to const int const int *AR_LISTF for each of the array elements (this is because C-style arrays are really pointers underneath). You could also put const int AR_LISTF[] which means the same thing but you might consider clearer in intention.
Last edited on
Thank you for your response.

When I removed the brackets from AR_LIST and NAME_ARRAY it produced the following error:

Invalid arguments ' Candidates are: void FindFirstNum(int, int,
std::basic_string<char,std::char_traits<char>,std::allocator<char>>) '

I'm not entirely sure what this error message means.
Did you change FindFirstNum to take pointers?
The error is saying it can't find a function called FindFirstNum that takes the kinds of arguments you are giving it. Put another way, you're giving it arrays of ints, but the function was expecting just plain ints.
Last edited on
Topic archived. No new replies allowed.