I got the error C2440: - c++

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];

Related

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

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.

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.

error: invalid conversion from 'char' to 'const char*' [-fpermissive]|

just a noob trying a programm
i am facing the error and i dont knpw how to resolve it
i wanted the functionto retuen one string that could be used for further use in main
char word_inventory(int ran)
{
char cities[][12]={
"ambala",
"yamunanagar",
"delhi",
"gurgaon",
"jaipur",
"chandigarh",
"pune",
"mumbai",
"dehradun",
"rajpura"
};
return cities[ran];
}
int main()
{ int win=0;
cout<<"welcome to hangman"<<endl;
cout<<"guess the city"<<endl;
randum = rand() % 10 + 0;
strcpy(word,word_inventory(randum)); //here is the error
x=strlen(word);
The immediate error is that cities[ran] is an array of chars, which does not match your function's return type. The array would decay to a pointer to the first element, so you could return char *.
The bigger problem is that the pointer wouldn't be valid, because the lifetime of array into which it points ends at the end of the function call. One solution is to make the array static, so it has permanent lifetime.

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.

String/char arrays inside struct

I am trying to create a struct that has multiple string arrays inside of it. For my purposes I wanted to use std::string arrays but char * arrays would also work if they can get the job done. Either way I can't figure out how to initialize things. This is what I have:
initialize.h
#include <string>
struct myStruct
{
std::string x[22];
std::string y[8];
};
extern myStruct data[22];
myform.cpp
#include <initialize.h>
#include <string>
myStruct data[22];
data[0].x = {"a", "b", "c", "d", ...};
I am getting errors that look like this:
Error 1 error C2059: syntax error : '{'
Error 2 error C2143: syntax error : missing ';' before '{'
Error 3 error C2143: syntax error : missing ';' before '}'
I have tried various permutations with char * arrays or std::string * arrays instead but to no avail, I am quite stuck. Did I forget something fundamental?
Thanks in advance.
You cannot use the { } array initialization syntax to assign values to arrays. It can only be used when initializing the array right after definition:
int a[3] = { 3, 4, 5 };
but not
int a[3];
a = { 3, 4, 5 }; //error
you will not get around a loop or a manual initialization of every member. But I think the new C++0x standard improves upon this and makes this (and even more initializer syntax) possible.
myStruct data[22];
With the above statement, you have already created 22 objects of type myStruct and each object having it's own x,y string arrays whose size is 22,8 respectively.
You can only initialize each member of an array directly only while declaration and not during assignment operation. So, you can try -
data[0].x[0] = "a";
data[0].x[1] = "b";
// ....
What the error you are doing is something similar to -
int a[5] ;
a = { 1,2,3,4,5 } ; // Error.
int a[] = { 1,2,3,4,5 } ; // Correct.
The
{"a", "b", "c", "d", ...};
syntax is only allowed when defining a variable, so you could do
std::string data[4] = {"a", "b", "c", "d"}; // syntax allowed for definition
but the line
data[0].x = {"a", "b", "c", "d", ...}; // not definition
is not a definition (data[0].x isn't a new variable). Moreover, since this is not a definition, you can't actually place this code outside of a function.
Somewhere in code you're going to have to manually assign each variable (as #Mahesh's answer suggests)
It's not possible to initialize non static array members in C++. Sorry.
If you make these char * instead of strings, you could get away with a static initializer. It's going to be long and ugly though.
struct myStruct
{
char * x[22];
char * y[8];
};
extern myStruct data[22];
myStruct data[22] = {
{ // data[0]
{ "a", "b", "c", ... "v" }, // data[0].x
{ "0", "1", ... "7" } // data[0].y
},
{ // data[1]
...
};
The other comments are correct but I believe there is one other thing you can do. You can initialize the structure when you declare it in your header:
struct myStr
{
string x[22];
string y[8];
} data[22] = { {...}, {...}, ... };
This too will be long and ugly but might address your question. As others have said, you can't extern this and assign to it after instantiation.