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.
Related
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);
}
Here a basic code I'm trying to run But I'm having trouble with stoi (it's c++) I keep getting error:
‘stoi’ was not declared in this scope
I tried atoi and strtol with this error
.cpp:23: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int atoi(const char*)’
The code:
using namespace std;
int main(){
string numberGuessed;
int intNumberGuessed = 0;
do {
cout << "Guess a numeber btw 1 - 10: " << endl;
getline(cin, numberGuessed);
intNumberGuessed = atoi(numberGuessed);
cout << intNumberGuessed << endl;
} while(intNumberGuessed != 4);
cout<< "you win" << endl;
return 0;
}
The atoi() function accepts const char* argument, but you're trying to pass it std::string. Write it like intNumberGuessed = atoi(numberGuessed.c_str()); to take the pointer.
As for the first error, about stoi() being undeclared — it is because the function was added in C++11 standard, so you have to enable its support in your compiler. I.e. in older versions of GCC you could do it with -std=c++11 option (since gcc5 C11 is enabled by default, and since gcc6 C++11 will be enabled by default).
Use stoi, it's the modern C++ version of C's atoi.
Update:
Since the original answer text above the question was amended with the following error message:
‘stoi’ was not declared in this scope
Assuming this error was produced by g++ (which uses that wording), this can have two different causes:
Using a non-conforming variant of g++ that doesn't provide std::stoi.
Using g++ in C++03 mode (stoi was introduced in C++11).
For Windows, the MinGW-w64 variant is known to provide std::stoi, and in particular the Nuwen distribution is based on MinGW-w64.
For C++11 mode, with g++ use the option -std=c++11. For example, this is necessary with the Nuwen distribution g++ version 5.1.
I am trying to do the following operation:
source = new int[10];
dest = new int[10];
std::copy( std::begin(source), std::end(source), std::begin(dest));
However, the compiler reports the following error.
copy.cpp:5434:14: error: ‘begin’ is not a member of ‘std’
copy.cpp:5434:44: error: ‘end’ is not a member of ‘std’
copy.cpp:5434:72: error: ‘begin’ is not a member of ‘std’
I have included the required <iterator> header in the code. Can anybody help me on this?
Template functions std::begin() and std::end() are not implemented for pointers (pointers do not contain information about the number of elements they refer to) Instead them you should write
std::copy( source, source + 10, dest);
As for the error you should check whether you included header
#include <iterator>
Also maybe your compiler does not support the C++ 2011 Standard.
In addition to include <iterator>in C++11 enabled compiler. You should know begin/end are not useful for pointers, they're useful for arrays:
int source[10];
int dest[10];
std::copy(std::begin(source), std::end(source), std::begin(dest));
also have this problem when using g++ compiler this code in linux.
Using g++ compiler that contain C++ featuer should add C++11 flag
g++ -std=c++11 -o test test.cpp
In the following code from Stroustrup's book we are warned against an error from a narrowing conversion which does not occur on my version GCC (4.7.2)
#include <iostream>
using namespace std;
int main()
{
int i1 = 7.2;
int i2{7.2};
cout << i1 << " " << i2 << endl;
return 0;
}
As demonstrated here at ideone is this a bug or am I not running with the appropriate command line arguments to the compiler? Or is this meant to be a purely semantic error?
With g++ 4.8.1:
foo.cpp: In function 'int main()':
foo.cpp:8:15: warning: narrowing conversion of '7.2000000000000002e+0' from 'double' to 'int' inside { } [-Wnarrowing]
int i2{7.2};
^
I assume it's simply a bug in 4.7.2's c++11 support.
I get a narrowing warning with GCC 4.7.3 when I use -std=c++0x:
g++ -std=c++0x test.cpp
test.cpp: In function ‘int main()’:
test.cpp:8:23: warning: narrowing conversion of ‘7.2000000000000002e+0’ from ‘double’ to ‘int’ inside { } [-Wnarrowing]
g++ --version
g++ (GCC) 4.7.3
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.