error: ‘make_reverse_iterator’ is not a member of ‘std’ - c++

I'm trying to compile a c++ program which uses std::make_reverse_iterator, but I get the title error. The reference says that the function is indeed supported since c++14, so I added the flag -std=c++14, but still no dice. I've been digging around for a while and can't figure out why this is happening. Any ideas? My compiler version is g++ 4.9.2
Here's a simple program (taken from the above ref), the commands used to compile it, and the program's output.
test.cpp:
#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
int main() {
auto v = std::vector<int>{ 1, 3, 10, 8, 22 };
std::sort(v.begin(), v.end());
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, ", "));
std::cout << '\n';
std::copy(
std::make_reverse_iterator(v.end()),
std::make_reverse_iterator(v.begin()),
std::ostream_iterator<int>(std::cout, ", "));
}
Compiler:
g++ -std=c++14 test.cpp -o test
Output:
test.cpp: In function ‘int main()’:
test.cpp:15:9: error: ‘make_reverse_iterator’ is not a member of ‘std’
std::make_reverse_iterator(v.end()),
^
test.cpp:16:9: error: ‘make_reverse_iterator’ is not a member of ‘std’
std::make_reverse_iterator(v.begin()),

make_reverse_iterator() was added as a result of LWG 2285 and wasn't added to libsdtc++ until December 2014. It's in 5.1 release, and everything there. 4.9.2 was just too early to have it. So if possible, just upgrade.
If not possible, this is something that you can add yourself by just copying the cppreference implementation:
template< class Iterator >
std::reverse_iterator<Iterator> make_reverse_iterator(Iterator i)
{
return std::reverse_iterator<Iterator>(i);
}

Related

std::optional does not compile

This code does not compile with the command g++ -std=c++17 main.cpp
#include <iostream>
#include <experimental/optional>
int main()
{
std::optional<int> x;
std::cout << "Hello World";
return 0;
}
The Errors are the following:
error: ‘optional’ is not a member of ‘std’
error: expected primary-expression before ‘int’
Is there a way to get this code to compile?
The <experimental/optional> header doesn't define std::optional but rather std::experimental::optional. To get std::optional, which is a (non-experimental) part of the C++17 standard, you should just #include <optional>.
Try on godbolt.

How can I use stl iterators with Eigen?

I am trying to use the Library Eigen in a project and I have to sort a vector. I tried to follow the documentation and it says that the library should work in the predictable way with the STL iterators and algorithms https://eigen.tuxfamily.org/dox-devel/group__TutorialSTL.html.
However when I try to run the following test code
#include <iostream>
#include <algorithm>
#include <eigen3/Eigen/Dense>
#include <eigen3/Eigen/Core>
int main()
{
Eigen::Array4i v = Eigen::Array4i::Random().abs();
std::cout << "Here is the initial vector v:\n" << v.transpose() << "\n";
std::sort(v.begin(), v.end());
std::cout << "Here is the sorted vector v:\n" << v.transpose() << "\n";
return 0;
}
I get the two following errors:
error: ‘Eigen::Array4i’ {aka ‘class Eigen::Array<int, 4, 1>’} has no member named ‘begin’
9 | std::sort(v.begin(), v.end());
| ^~~~~
error: ‘Eigen::Array4i’ {aka ‘class Eigen::Array<int, 4, 1>’} has no member named ‘end’
9 | std::sort(v.begin(), v.end());
I tested it with gcc 9.1.0 and 7.4.0 and my version of Eigen is 3.3.4. I am using Ubuntu 18.04 and the library is in the usual location /usr/include. All the other functionality I tried seem to work properly.
Is it a well known bug, is it a compiler problem or is it a version problem?
If you don't want to wait for the release of Eigen 3.4, you can use this:
std::sort(v.data(), v.data() + v.size());
The .data() method can replace the missing .begin(), but the .end() method must be constructed manually.

Is this (auto && elem : v) syntax simple C++? what compiler should I use to have it compile?

I used to code in C++ 12 years ago, but left it for other simpler languages due to my job.
I'd like to renew my knowledge and tried to compile the solution proposed here, just to try this new way to iterate on vectors. But ran into a compile error:
expected initializer before ‘:’ token
I didn't know it was possible to avoid explicit declaration of iterators like that in C++ with the use of this (auto && elem : v). What version of C++ is it?
b.cpp
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <string>
#include <set>
int main()
{
std::vector<std::pair<std::string, std::string>> v
{ {"handgun", "bullets"},
{"turret", "bullets"}};
std::cout << "Initially: " << std::endl << std::endl;
for (auto && elem : v)
std::cout << elem.first << " " << elem.second << std::endl;
return 0;
}
Compilation
$ cc b.cpp -std=c++0x -o myprog
Errors
b.cpp: In function ‘int main()’:
b.cpp:15: error: expected initializer before ‘:’ token
...
C++ Compilers I use: g++
$ g++ --version
gives
g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-3)
Copyright (C) 2010 Free Software Foundation, Inc.
Your compiler is too old. Ranged-based for loops weren't added until 4.6.
Also, ignore the comments. cc is usually symlinked to gcc. From the manual:
GCC recognizes files with these names and compiles them as C++
programs even if you call the compiler the same way as for compiling C
programs (usually with the name gcc).
Following pupper's advice, I adapted my code to the below:
#include <iostream>
#include <vector>
int main()
{
std::vector<std::pair<std::string, std::string>> v
{ {"handgun", "bullets"},
{"turret", "bullets"}};
std::cout << "started: " << std::endl;
std::vector<std::pair<std::string, std::string>>::iterator Current_Iterator = v.begin();
while(Current_Iterator != v.end())
{
std::cout << Current_Iterator->first << " " << Current_Iterator->second << std::endl;
Current_Iterator++;
}
return 0;
}
It still doesn't compile with the gcc on my system, giving
b.cpp:(.text+0x134): undefined reference to ``std::cout'
...
But it works with g++:
$ g++ b.cpp -std=c++0x -o myprog && ./myprog
started:
handgun bullets
turret bullets

Using string.pop_back() and string.back()

I'm trying to use the string member functions back() and pop_back(), but I think my compiler doesn't recognize them as members. However, when I use the option -std=c++0x, the compiler stops complaining only about back(), but still complains about pop_back(). Here are the code and compiler errors:
Test.cpp:
#include <iostream> // std::cout
#include <string> // back(), pop_back()
int main()
{
std::string str ("Optimist!");
std::cout << str.back() << "\n";
str.pop_back();
std::cout << str << "\n";
}
g++ Test.cpp:
Test.cpp: In function ‘int main()’:
Test.cpp:8:20: error: ‘std::string’ has no member named ‘back’
Test.cpp:9:7: error: ‘std::string’ has no member named ‘pop_back’
g++ -std=c++0x Test.cpp:
Test.cpp: In function ‘int main()’:
Test.cpp:9:7: error: ‘std::string’ has no member named ‘pop_back’k’
How can I use these functions with g++?
EDIT: Using g++ 4.6.3
Both std::basic_string::pop_back() and std::basic_string::back() were added to C++11. Without explicitly using the C++11 standard with -std=c++11, you won't be able to use these functions at all.
The current version of GCC is 4.8.2. Some earlier versions may not have one or both of these functions implemented. You should upgrade to 4.8.2 if you need them.

Using std::to_wstring with libstdc++?

Why this doesn't compile on gcc?
#include <iostream>
#include <string>
int main() {
double f = 23.43;
std::wstring f_str = std::to_wstring(f);
std::wcout << f_str << std::endl;
return 0;
}
The error:
prog.cpp: In function ‘int main()’:
prog.cpp:6:26: error: ‘to_wstring’ is not a member of ‘std’
example
std::to_wstring() was added in C++11. The link in your answer, on ideone, is not using the C++11 compiler:
language: C++ (gcc-4.7.2)
It compiles fine: http://ideone.com/UO2FQM :
language: C++0x (gcc-4.7.2)
Your program compiles IF you choose C++0x in IDEONE instead of C++ because its a C++11 feature.