expected primary-expression before ‘]’ token myArray <int> obj(arg1[], size); Confused as to why this is happening

The code is supposed to print out the highest value in an array

The error is also occurring in my constructor for list[] = arg[]



#include <iostream>
using namespace std;

template <class T>
class myArray{
T list[];
T size;
public:
myArray(T arg[], T x){list[] = arg[], size = x;}
T array_max();
};

template <class T>
T myArray<T>::array_max(){
T index = 0;
for(int i = 0; i < size; i++){
if(list[i] > list[i+1]){
list[i+1] = list[i];
index = i;
}
}
return list[index];
}

int main(){
int temp = 0;
int size = 5;
int arg1[size] = {4, 5, 10, 12, 9};
myArray <int> obj(arg1[], size);
temp = obj.array_max();

cout << temp;

}
Last edited on
You just pass arg1, not arg1[]

And "list[] = arg[]" doesn't make sense. You can't assign arrays like this.

Okay let's back up. If you're trying to put an array inside something else, like a class, there are more restrictions on what you can do. Because an array needs to have a size. It can't dynamically be assigned a new one. If you need to do that, use vector.

Instead of passing the size as a parameter, since you're using template, perhaps you should have size as a template parameter.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

template <class T, int Size>
class myArray{
    T list[Size];
    
public:
    myArray(T arg[], T size){
        for (int i = 0; i < size; i++)
        {
            list[i] = arg[i];   
        }
    }
};

int main(){
    int temp = 0;
    
    const int Size = 5;
    int arr[Size] = {4, 5, 10, 12, 9};
    
    myArray<int, Size> obj(arr, Size);
}
Last edited on
Thank you for your help. I figured my initialization of the list was wrong.
Please use code tags when posting code so that the code is readable.


[code]
   code goes here
[/code]


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
#include <iostream>
using namespace std;

template <class T>
class myArray {
	T list[];
	T size;
public:
	myArray(T arg[], T x) { list[] = arg[], size = x; }
	T array_max();
};

template <class T>
T myArray<T>::array_max() {
	T index = 0;
	for (int i = 0; i < size; i++) {
		if (list[i] > list[i + 1]) {
			list[i + 1] = list[i];
			index = i;
		}
	}
	return list[index];
}

int main() {
	int temp = 0;
	int size = 5;
	int arg1[size] = {4, 5, 10, 12, 9};
	myArray <int> obj(arg1[], size);
	temp = obj.array_max();

	cout << temp;

}

Maybe something like:

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

#include <iostream>
#include <algorithm>

template <class T, size_t SIZE>
class myArray {
	T list[SIZE] {};

public:
	myArray(const T arg[SIZE]) { std::copy(arg, arg + SIZE, list); }

	T array_max() const;
};

template <class T, size_t SIZE>
T myArray<T, SIZE>::array_max() const {
	T maxVal {list[0]};

	for (size_t i {1}; i < SIZE; ++i)
		if (list[i] > maxVal)
			maxVal = list[i];

	return maxVal;
}

int main() {
	constexpr size_t size {5};
	constexpr int arg1[size] {4, 5, 10, 12, 9};

	const myArray <int, size> obj(arg1);

	std::cout << "Max value is " << obj.array_max() << '\n';
}

Topic archived. No new replies allowed.