I have a problem initializing an Array

I've declared an array in a class and Visual Studio displays an error saying "Too many initializer values".

SIZE is a constant value that equals to 10, and the arrays have 10 zero's.

1
2
3
4
5
6
7
8
9
10
11
#include "Unique.h"
#include "Constants.h"

#include <iostream>

Unique::Unique()
{
	m_UniqueNumbers[SIZE] = {0,0,0,0,0,0,0,0,0,0};
	m_Repeats[SIZE] = {0,0,0,0,0,0,0,0,0,0};
	m_Counter = 0;
}


Before I tried assigning each array to {}; , but Visual Studio told me that 10 is out of range '0' to '9', and that the writable size is '40 bytes', but '44' might be written

1
2
3
4
5
6
7
8
9
10
11
12
#include "Unique.h"
#include "Constants.h"

#include <iostream>

Unique::Unique()
{
	m_UniqueNumbers[SIZE] = {};
	m_Repeats[SIZE] = {};
	m_Counter = 0;
}


Can you help me please.
Last edited on
I see no initialization here. I see attempts to assign to existing variables.
1
2
3
4
5
6
7
8
9
10
Unique::Unique()
 : m_Counter(7) // member initializer list
{
  m_counter = 42; // assignment
}

// corresponds to:

int m_counter = 7; // initialization with value 7
m_counter = 42; // assignment 


C++11 allows initialization of array members too. See: https://www.informit.com/articles/article.aspx?p=1852519
its easier to say
int foo[] {1,2,3,4,5,6,7,8,9,10}; //foo auto sizes to fit, but it is still constant sized. I don't think you need the = sign here, I forget.

int foo[10]{}; //all zeros. no need for = for sure

as noted your bug is not having a type on it.
const size = 10;
int foo[size] = {1,2,3,4,5,6,7,8,9,10}; //ok, initialize
foo[size] = {1,2,3,4,5,6,7,8,9,10}; //error, 10 is too big, on top of other woes.

What are the definitions for m_UniqueNumbers and m_Repeats
jonnin wrote:
as noted your bug is not having a type on it.

That depends.

* If the m_* in the body of a class constructor are local automatic variables and the statements are supposed to be variable declarations, then yes, these declarations lack type.

* If the m_* are member variables of class Unique, then the statements are malformed assignments.

1
2
3
4
5
6
int array[SIZE];
array[SIZE] = 42; // assignment. Out of range error, the last element is array[SIZE-1]

array[SIZE] = {42, 7}; // two errors. Out of range and
// left side is one int
// right side is array of int 

Last edited on
Topic archived. No new replies allowed.