This question already has an answer here:
vector cannot be initialized by {}
(1 answer)
Closed 7 years ago.
I am trying to initialize map in the following way, but I'm not able to do so.
Could you please give me a suggestion?
map<char , int> err_codes = {{'a',1},{'b',0}};
ERROR : initialization with '{...}' is not allowed for object of type
"std::map, std::allocator>>"
It seems your compiler does not support this C++ 2011 feature of list initializations for standard containers. Check the compiler documentation how to switch on the support of C++ 2011 if it is possible.
Related
This question already has an answer here:
Range-based for loop without specifying variable type
(1 answer)
Closed 2 years ago.
I'm fairly new to C++. So today while solving a problem I wrote a code like this -
for(i : {1,2,3}) {
cout << i << "\n";
}
And it compiled and ran just fine. After a while I noticed that I didn't specify any type for "i" and it didn't show me any error. I want to know, how C++ handles this? What type is set for the variable "i" internally?
It seems you are using a non-standard compliant compiler: Range-based for loop without specifying variable type
For example, gcc seems to have dropped support for this unofficial construct between versions 5.5 and 6.1: https://godbolt.org/z/8CJGim
This question already has answers here:
Why does C++ allow us to surround the variable name in parentheses when declaring a variable?
(2 answers)
Closed 5 years ago.
I am new to C++, I see following syntax in c++ to initialize variable.
int(i)=1;
Then, I have compiled in G++ compiler and compiler did not give any error or warning.
So, What does mean int(i)=1; in C and C++?
Also, I have tested in C, I thought, the C compiler give an error but it's also working fine.
It's basically a strange way to write
int i = 1;
Nothing to worry about.
Sometimes, parenthesis around the variable name are necessary in defintions (eg. pointer to functions), and there is no reason to prohibit them for other cases, so it's allowed without any deeper reason.
Maythe the author didn't like spaces (such people exist).
This question already has answers here:
How do I enable C++11 in gcc?
(4 answers)
why i cannot use the auto keyword in the last version of gcc
(5 answers)
Closed 5 years ago.
consider following program
string hr = "hackerrank";
for(auto &c: hr){
cout<<c;
}
this code runs perfectly fine on hackerrank but whenever I try to compile this program on my ubuntu 16.4.1 using g++ compiler, it shows following error,
error: ISO C++ forbids declaration of ācā with no type [-fpermissive]
for(auto &c: hr){
why is it showing error on g++ compiler but not on HackerRank ?
PS: I tried going through all questions where this error is discussed. but I couldn't find an answer. If you find a similar question then let me know.
This question already has answers here:
Error: Label used but not defined when using && operator [closed]
(3 answers)
Closed 8 years ago.
Can anyone explain how the following code creates a label?
char memory[] = "hello";
&&memory[0];
error: label 'memory' used but not defined
&&memory[0];
That's not valid C++, thus a conforming extension can assign any semantics one could want to it.
It so happens, that &&label is the GNU folks' way of taking the address of a label for computed goto's, a GNU extension.
That's it.
Reference: https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html
This question already has answers here:
Why cast an unused function parameter value to void?
(2 answers)
Closed 8 years ago.
I saw following code few times
void func(SomeTypeEGInt varname) {
(void)varname;
}
I wish to know what it means and why people implement such functions.
It tell the compiler that those variables are unused. It is used to prevent the warnings which you will get.
The (void)varname; pattern is typically used to silence compiler warning about unused arguments. So this example is actually an empty function which does nothing.