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.
Related
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.
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);
}
I'm trying to compile a simple program utilizing literals from the std::literals namespace, but Clang is generating errors when I try to compile it.
The code I'm trying to compile:
#include <string>
#include <iostream>
using namespace std::literals;
int main()
{
std::cout << "Hello World!"s << std::endl;
return 0;
}
and the compilation command:
clang++ -stdlib=libstdc++ -std=c++1y a.cpp
which leads to this output:
a.cpp:4:22: error: expected namespace name
using namespace std::literals;
~~~~~^
a.cpp:8:29: error: no matching literal operator for call to 'operator "" s' with arguments of
types 'const char *' and 'unsigned long', and no matching literal operator template
std::cout << "Hello World!"s << std::endl;
^
2 errors generated.
Using g++ or libc++ are out of the question for various reasons, and I've confirmed that other C++14 features (ie. return type deduction and binary literals) work, so it's not an issue with the compiler, making me believe it involves libstdc++.
What can I do to fix this? I'm on Linux Mint 17.1 if it makes any difference.
Remember to ensure that you're compiling the source according to C++14 (the chrono literals are not provided in C++11).
clang++ -stdlib=libstdc++ -std=c++14 a.cpp
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.
I am completely new to C++ and I am now following the C++ Primer book.
I wrote a small example about strings, here is the code:
#include <iostream>
#include <string>
#include <cctype>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main() {
string s("Hello World");
for (auto &c : s)
c = toupper(c);
cout << s << endl;
return 0;
}
I am on Linux with GCC version 4.4.6 and I tried to compile this code with:
g++ test_strings.c -std=c++0x
but got the following errors:
test_strings.c: In function 'int main()':
test_strings.c:14: error: expected initializer before ':' token
test_strings.c:19: error: expected primary-expression before 'return'
test_strings.c:19: error: expected ')' before 'return'
I copied the program from the textbook, so I though it was a misspelling but after a check and trying searching on the web and updating my gcc the error reminds. Help will be greatly appreciated, thanks in advance.
As per the C++0x/C++11 Support in GCC page, you need to be running gcc 4.6 to get the range-for feature.
The 4.6 changes page contains:
Improved experimental support for the upcoming C++0x ISO C++ standard, including support for constexpr (thanks to Gabriel Dos Reis and Jason Merrill), nullptr (thanks to Magnus Fromreide), noexcept, unrestricted unions, range-based for loops (thanks to Rodrigo Rivas Costa), opaque enum declarations (thanks also to Rodrigo), implicitly deleted functions and implicit move constructors.
Since you're running gcc 4.4.6, it's not available to you.