C++ error: too many initializers for 'int [2]' - c++

I keep getting this error when declaring an int[2] array, but it looks fine to me.
error: too many initializers for 'int [2]'
int <array_name>[2] = { 0, 255, 255 };
^
am I doing someting wrong?

You declared the size of array is 2 but gave it 3 elements, I think just change it to int <array_name>[3] will fix the problem

You declare the array to have two elements with [2], but you're trying to assign three numbers to it with { 0, 255, 255 }. Something's gotta give.

Related

conversion from 'unsigned int' to 'int' requires a narrowing conversion

My code includes the following, and I get the error message above based on the last line below.
struct List {
int word_i;
int mod_i;
char mod_type;
char mod_char;
};
struct Morph {
Options mode;
deque<List> search_list;
vector<string> dictionary;
vector<bool> discovered;
string output;
int sel_word_i = 0;
bool end_found = 0;
};
// later on in a function:
morph->search_list.push_back({ morph->dictionary.size() - 1, 0, 0, 0 });
You can replace the last line with:
morph->search_list.emplace_back( morph->dictionary.size() - 1, 0, 0, 0 );
Thus the object is created not through brace initialization which does not allow narrowing conversion.
The narrowing conversion is from the return value of the call to size which returns std::size_t which is unsigned.
For why size() - 1 is not converted to a signed value see: C++ Implicit Conversion (Signed + Unsigned)
When and after you applied what Amir suggested, you may get an error saying something like, "this function does not take (3) arguments." To fix that you'll have to declare a constructor in the class, which you used for your vector, that takes that particular number of arguments. From what I understood when you replace push_back(); with emplace_back();the compiler thinks that you're trying to pass some variables to the constructor, which are the supposed arguments.

no matching function for call to && no known conversion for argument 1 from

So I've been trying to test my code in main but I can't get past this error
In my .h file I have this declared this method
int common(string words[], int count[], int n);
My .cpp file
int testclass::common(string words[], int count[], int n){
//code
}
This method will take the string and int arrays as parameters and populate them based on a known array and amount n.
For my main function I have something like this
testclass test
string testwords[4] = {"0", "0", "0", "0"};
int testcount[4] = {0, 0, 0, 0};
test.common(testwords[4], testcount[4], 4);
I've also tried to put in the arrays without initializing
string testwords[4];
int testcount[4];
I can't get past this error
In function 'int main()*':
error: no matching function for call to 'testclass::common(std::__cxx11::string&, int &, int)'
test.common(testwords[4], testcount[4], 3);
^
//in cpp file
note: candidate: int testclass::common(std::__cxx11::string*, int*, int)
int testclass::common(string words[], string count[], int n)
^
note: no known conversion for argument 1 from 'std::cxx11::string {aka std::__cxx11::basic_string<char>}' to 'std::__cxx11::string* {aka std::__cxx11::basic_string<char>*}'
Passing testwords[4] to the function tries to select a single word from the array. And out of bounds at that.
To pass an array as a pointer (or as x[]), just give the name of the array:
test.common(testwords, testcount, 4);
The array name will decay to a pointer in almost all cases, including this.
Mistake you are making is in function call
test.common(testwords[4], testcount[4], 4);
Try this instead
test.common(testwords, testcount,4);
But you need to pass the array size as different parameter

Wrong use of typedef of typedef(?)

I have problem with code from book:
const int SQUARE_ARRAY_SIZE = 4;
const int SQUARE_INFO_SIZE = 4;
typedef Square SquareArray[SQUARE_ARRAY_SIZE];
typedef SquareArray SquareInfo[SQUARE_INFO_SIZE];
SquareArray RedGeneric = { Square(0, 0), Square(0, 1),
Square(1, 1), Square(1, 0) };
SquareInfo RedInfo = { &RedGeneric, &RedGeneric, \\problem here
&RedGeneric, &RedGeneric };
It yells:
error C2440: 'initializing' : cannot convert from 'SquareArray (*)' to 'Square'
IntelliSense: no suitable constructor exists to convert from "SquareArray *" to "Square"
As I understand SquareInfo stands for array of SquareArray but it seems like vs2013 wants to break it to Squares and as result
SquareInfo m_squareInfo; ...
SquareArray* pSquareArray = m_squareInfo[m_iDirection];
yells:
IntelliSense: a value of type "const Square *" cannot be used to initialize an entity of type "SquareArray *"
Book is from 2008 and I don't know if it worked back then or there is error from beginning. As for q please tell me what's really wrong and how to make it work.
I'm fairly sure that the book meant to write
typedef SquareArray* SquareInfo[SQUARE_INFO_SIZE];
// ^
i.e., SquareInfo is an array of pointers to SquareArrays, rather than an array of SquareArrays. This is consistent with initializations using &RedGeneric and SquareArray* pSquareArray = m_squareInfo[m_iDirection];.
Looks like your book has a typo on that line
typedef SquareArray SquareInfo[SQUARE_INFO_SIZE];
When I make this to be a pointer
typedef SquareArray* SquareInfo[SQUARE_INFO_SIZE];
// ^
the code compiles fine.

I got the error C2440:

I got the following error
error C2440: 'initializing' : cannot convert from 'const int' to 'int
[16]'
My code is like this
static int Count[MAX_STATION_NO] = 0;
I got error on above line. Can someone tell me what is the problem in the above line?
You're treating an array as a pointer, which is illegal. You can't assign an array to a value.
Perhaps you want this:
static int Count[MAX_STATION_NO] = {0};
You are creating an array and setting the arrays value to '0'. What I think you wish to do is:
static int Count[MAX_STATION_NO] = {0}
This line is declaring an array of size 16, then you are assigning a single number to it, which is not syntactically correct. You need to use an array initializer:
{ 16, 2, 77, 40, 12071 ... }
Curly brace {} are required to initialize arrays.
eg:
static int Count[MAX_STATION_NO]={1,2,3};
Maybe this link could help you: http://www.cplusplus.com/doc/tutorial/arrays/
Your initializer needs to be in braces:
static int Count[MAX_STATION_NO] = { 0 };
Btw: static arrays have their content initialized with 0 anyway so you the above is equivalent to:
static int Count[MAX_STATION_NO];

What's wrong with this array declaration?

I created this array this array inside a function, with the variable MODEL_VERTEX_NUM initialized # runtime, which, I guess, is the sticking point here.
loat model_vertices [][]= new float [MODEL_VERTEX_NUM][3];
I got the following errors:
1>.\GLUI_TEMPLATE.cpp(119) : error C2087: 'model_vertices' : missing subscript
1>.\GLUI_TEMPLATE.cpp(119) : error C2440: 'initializing' : cannot convert from 'float (*)[3]' to 'float [][1]'
I realize that when I do:
float model_vertices *[]= new float [MODEL_VERTEX_NUM][3];
The compiler lets this pass, but I wanna understand what's wrong with the previous declaration.
So, what's wrong with the [][] declaration?
For a two-dimensional array a[X][Y] the compiler needs to know Y to generate code to access the array, so you need to change your code to
float (*model_vertices) [3] = new float [2][3];
If you have an array of type T a[X][Y] and want to access a[x][y] that is equivalent to accessing *(((T*)(&a[0][0])) + x*Y + y). As you can see the compiler needs to know Y but not X to generate code for accessing the array.