initializer list as argument to operator[] - c++

This question is related to the one discussed here.
I try to use an initializer list to create an argument to be passed to operator[].
#include <string>
#include <vector>
struct A {
std::string& operator[](std::vector<std::string> vec)
{
return vec.front();
}
};
int main()
{
// ok
std::vector<std::string> vec {"hello", "world", "test"};
A a;
// error: could not convert '{"hello", "world", "test"}' to 'std::vector...'
a[ {"hello", "world", "test"} ];
}
My Compiler (GCC 4.6.1) complains:
g++ -std=c++0x test.cpp
test.cpp: In function ‘int main()’:
test.cpp:20:8: error: expected primary-expression before ‘{’ token
test.cpp:20:8: error: expected ‘]’ before ‘{’ token
test.cpp:20:8: error: expected ‘;’ before ‘{’ token
test.cpp:20:35: error: expected primary-expression before ‘]’ token
test.cpp:20:35: error: expected ‘;’ before ‘]’ token
Should this be valid C++11?
Interestingly, when using operator() instead of operator[] it works.

Yes, it is valid C++11 and should work in any compliant compiler.
Please note that C++11 support in gcc is quite immature, and this code example will not compile in any 4.6 version, but only in 4.7 svn snapshot versions.

Related

Using unordered_map on my g++ (5.1.0) compiler in command prompt shows error

I have recently downloaded MinGW into my computer but on using certain containers and iterators like unordered_map and auto it shows an unexpected error.
my code is as follows :
#include <bits/stdc++.h>
#include<unordered_map>
using namespace std;
int main()
{
unordered_map<string, int> umap;
umap["GeeksforGeeks"] = 10;
umap["Practice"] = 20;
umap["Contribute"] = 30;
for (auto x : umap)
cout << x.first << " " << x.second << endl;
return 0;
}
it gives the following error :
C:\Users\naima\Documents\cpp>g++ -o try2 try2.cpp
In file included from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/unordered_map:35:0,
from try2.cpp:2:
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support for the \
^
try2.cpp: In function 'int main()':
try2.cpp:9:5: error: 'unordered_map' was not declared in this scope
unordered_map<string, int> umap;
^
try2.cpp:9:25: error: expected primary-expression before ',' token
unordered_map<string, int> umap;
^
try2.cpp:9:27: error: expected primary-expression before 'int'
unordered_map<string, int> umap;
^
try2.cpp:11:5: error: 'umap' was not declared in this scope
umap["GeeksforGeeks"] = 10;
^
try2.cpp:15:15: error: 'x' does not name a type
for (auto x : umap)
^
try2.cpp:19:5: error: expected ';' before 'return'
return 0;
^
try2.cpp:19:5: error: expected primary-expression before 'return'
try2.cpp:19:5: error: expected ';' before 'return'
try2.cpp:19:5: error: expected primary-expression before 'return'
try2.cpp:19:5: error: expected ')' before 'return'
The compiler told you exactly what was wrong. It usually will.
This file requires compiler and library support for the ISO C++ 2011 standard. This
support is currently experimental, and must be enabled with the -std=c++11 or
-std=gnu++11 compiler options.
You just have to compile with the proper flag, -std=c++11. I don't know if you are version-matching against what graders use or what, but there are very few good reasons to be on a minGW compiler where support for an 8 year old standard is still considered experimental.
You can see that it works as expected here: https://godbolt.org/z/JQxL00
If you remove the -std=c++11 flag, it will fail to compile and give you the same error message.
You might also notice that I altered the includes to only include what I use. This results in a much faster compile time, smaller executable, and an easier to understand piece of code (Since it is plain to see what standard features are being used). You also avoid polluting your namespace.

What is the meaning of the address symbol & in a constructor?

I'm looking up c++ library, and see the istream class, I am confused with a contractor with an address symbol. what is the meaning of a constructor with an address symbol?
one of the istream constructors is.
protected: iostream& (iostream&& x);
I found it in website cplusplus.com,
link: iostream
I defined a customer class with a similar constructor that has a & symbol:
//Test.cpp
#include <iostream>/*cout,cin*/
#include <typeinfo>/*typeid(),name()*/
using namespace std;
struct MyTest{
MyTest&(double b){}
};
int main(int argc,char* argv[]){
MyTest mt2(2.1);
cout << typeid(mt2).name() << endl;
return 0;
}
I use the below command to compile it:
g++ Test.cpp -o Test -std=c++11
however, I get some compile error messages:
Test.cpp:7:11: error: expected unqualified-id before ‘float’
MyTest&(float b){}
^
Test.cpp:7:11: error: expected ‘)’ before ‘float’
Test.cpp:7:10: error: expected ‘;’ at end of member declaration
MyTest&(float b){}
^
Test.cpp:7:17: error: expected ‘;’ at end of member declaration
MyTest&(float b){}
^
Test.cpp:7:18: error: expected unqualified-id before ‘)’ token
MyTest&(float b){}
^
Test.cpp: In function ‘int main(int, char**)’:
Test.cpp:12:16: error: no matching function for call to ‘MyTest::MyTest(double)’
MyTest mt2(2.1);
I got confused, c++ library istream class is fine. why did my custom class constructor fail? what am I missing?
The information on cplusplus.com is... sometimes not dependable. (See What's wrong with cplusplus.com? for a discussion of this.) On CPPReference, you can see that the move constructor is, you know, just a regular move constructor.
This is a bug in http://www.cplusplus.com/reference/istream/iostream/iostream/.
If you look at https://en.cppreference.com/w/cpp/io/basic_iostream/basic_iostream, you will find
protected: basic_iostream( basic_iostream&& other );

Why does the following code work on online ide(gcc 7.2.0) but gives error on ubuntu?

When I run the following code on ubuntu(gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4)):
#include<iostream>
#include<vector>
#include<list>
using namespace std;
int main(){
vector <int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
list<int> temp;
for(auto i:v){
cout<<i<<" ";
temp.push_back(i);
}
for(auto i:temp){
cout<<i<<" ";
}
}
I get the following errors:
try.cpp: In function ‘int main()’:
try.cpp:13:10: error: ‘i’ does not name a type
for(auto i:v){
^
try.cpp:17:1: error: expected ‘;’ before ‘for’
for(auto i:temp){
^
try.cpp:17:1: error: expected primary-expression before ‘for’
try.cpp:17:1: error: expected ‘;’ before ‘for’
try.cpp:17:1: error: expected primary-expression before ‘for’
try.cpp:17:1: error: expected ‘)’ before ‘for’
try.cpp:17:10: error: ‘i’ does not name a type
for(auto i:temp){
^
try.cpp:20:1: error: expected ‘;’ before ‘}’ token
}
^
try.cpp:20:1: error: expected primary-expression before ‘}’ token
try.cpp:20:1: error: expected ‘;’ before ‘}’ token
try.cpp:20:1: error: expected primary-expression before ‘}’ token
try.cpp:20:1: error: expected ‘)’ before ‘}’ token
try.cpp:20:1: error: expected primary-expression before ‘}’ token
But when I run the code on online ide I works fine.
What is the problem with the code?
The link for code on online ide:No errors
Your code uses some of the C++11 features such as range based loops and auto specifier but you don't compile for the C++11 standard. You need to enable the C++11 support by including the -std=c++11 flag when compiling:
g++ -std=c++11 -o try try.cpp
The online compiler has this enabled by using the -std=gnu++1z flag.

C++ Missing Initializer error

I am getting this error during compile time (g++ 4.4.6):
main.cpp: In function ‘int main()’:
main.cpp:27: error: expected initializer before ‘:’ token
main.cpp:33: error: expected primary-expression before ‘for’
main.cpp:33: error: expected ‘;’ before ‘for’
main.cpp:33: error: expected primary-expression before ‘for’
main.cpp:33: error: expected ‘)’ before ‘for’
main.cpp:33: error: expected initializer before ‘:’ token
main.cpp:36: error: could not convert ‘((list != 0u) ? (list->SortedList::~SortedList(), operator delete(((void*)list))) : 0)’ to ‘bool’
main.cpp:37: error: expected primary-expression before ‘return’
main.cpp:37: error: expected ‘)’ before ‘return’
My code is as follows:
#include <iostream>
#include "Student.h"
#include "SortedList.h"
using namespace std;
int main() {
SortedList *list = new SortedList();
Student create[100];
int num = 100000;
for (Student &x : create) { // <--Line 27
x = new Student(num);
num += 10;
}
for (Student &x : create)
list->insert(&x);
delete list;
return 0;
}
Anybody who possibly knows the source of the error would be of great help. Also, Student and SortedList are objects which are declared in their .h files.
According to this page on GCC's website, range-based for is only available in g++ 4.6 and up, so you'll have to convert your code to a normal for loop or use std::for_each or something, or upgrade your compiler.

G++ doesn't compile C++0x range-based for loop

I was experimenting with some of the new C++0x features with G++. Lambdas, auto, and the other new features worked like a charm, but the range-based for-loop failed to compile. This is the program I was testing:
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> data = { 1, 2, 3, 4 };
for ( int datum : data )
{
std::cout << datum << std::endl;
}
}
I compiled it with:
g++ test.cpp -std=c++0x
I also tried gnu++0x, but the output was the same.
This was the output:
test.cpp: In function ‘int main()’:
test.cpp:8:21: error: expected initializer before ‘:’ token
test.cpp:12:1: error: expected primary-expression before ‘}’ token
test.cpp:12:1: error: expected ‘;’ before ‘}’ token
test.cpp:12:1: error: expected primary-expression before ‘}’ token
test.cpp:12:1: error: expected ‘)’ before ‘}’ token
test.cpp:12:1: error: expected primary-expression before ‘}’ token
test.cpp:12:1: error: expected ‘;’ before ‘}’ token
Thanks in advance for your help.
EDIT: I am using GCC version 4.5.2, which I now see is too old.
You need GCC 4.6 and above to get range-based for loops.
GCC's C++0x status
$ cat for.cpp
#include <iostream>
int main()
{
for (char c: "Hello, world!")
std::cout << c;
std::cout << std::endl;
return 0;
}
$ g++ -std=c++0x -o for for.cpp
$ ./for
Hello, world!
$ g++ --version
g++ (GCC) 4.6.1 20110325 (prerelease)