C++11 : error: ‘begin’ is not a member of ‘std’ - c++

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

Related

Which header file or definitions to use for using char8_t data type in C++?

So I want to use char8_t data type in my code. But my compiler shows me that it could not find an identifier like char8_t , which probably means it could not find required header file or definitions for it.
So can anyone tell me what header files / definitions should I use to get the char8_t data type? The language is C++. I'll be thankful if you can also answer for C.
PS:<cwchar> did not work.
Edit:
My code:
#include <iostream>
#include <cwchar>
using namespace std;
int main()
{
char c='a';
wchar_t w=L'A';
char8_t c8=u8'c';
char16_t c16=u'D';
char32_t c32=U'K';
cout<<"Data Type"<<"\t"<<"Size"<<"\n"
<<"char"<<"\t \t"<<sizeof(c)<<" bytes"<<"\n"
<<"wchar_t"<<"\t \t"<<sizeof(w)<<" bytes"<<"\n"
<<"char8_t"<<"\t"<<sizeof(c8)<<"\n"
<<"char16_t"<<"\t"<<sizeof(c16)<<" bytes"<<"\n"
<<"char32_t"<<"\t"<<sizeof(c32)<<" bytes"<<"\n";
return 0;
}
My compiler throws this error:
WideCharacters.cpp: In function 'int main()':
WideCharacters.cpp:8:5: error: 'char8_t' was not declared in this
scope; did you mean 'wchar_t'?
8 | char8_t c8=u8'c';
| ^~~~~~~
| wchar_t
WideCharacters.cpp:15:31: error: 'c8' was not declared in this
scope; did you mean 'c'?
15 | <<"char8_t"<<"\t"<<sizeof(c8)<<"\n"
| ^~
| c
Edit-2:
I have my C++ standard set to C++20 in VS code so there is no problem with standard.
char8_t is a keyword. It's built into the language, so you don't need any headers.
If it doesn't work, either your compiler doesn't support it, or you forgot to enable C++20 support (e.g. -std=c++20 in GCC).
The problem here is possibly the compiler is not able to identify that this is a C++20 feature.
Compile using this:
g++ -Wall -std=c++20 "yourFileName".cpp
replace youFileName with that of the file name.

boost::multiprecision::float128 and C++11

I'm trying to use boost::multiprecision::float128 (boost 1.55.0) under C++11 (gcc 4.8.1), but get the following compiler error:
/cm/shared/apps/boost/gcc/1.55.0/include/boost/multiprecision/float128.hpp: In static member function ‘static std::numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ET> >::number_type std::numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ET> >::min()’:
/cm/shared/apps/boost/gcc/1.55.0/include/boost/multiprecision/float128.hpp:533:55: error: unable to find numeric literal operator ‘operator"" Q’
static number_type (min)() BOOST_NOEXCEPT { return 3.36210314311209350626267781732175260e-4932Q; }
Can't I use boost::multiprecision::float128 in C++11? Or how else do I get it working?
edit
Just to clarify. This error is generated by
#include <boost/multiprecision/float128.hpp>
The compiler is not happy with the statement
return 3.36210314311209350626267781732175260e-4932Q;
in particular the Q is confusing it. I used the compiler flags -std=c++11 -fabi-version=0 -march=native -mfpmath=sse
It looks like a known issue. Try compiling with -fext-numeric-literals.

Why does -Wunused-variable in GCC produce an error even on static const?

I have a header, core/types.hh, used by several different build targets. It has the following declaration:
core/types.hh
typedef std::size_t Size;
static const Size SZ_MAX = std::numeric_limits<Size>::max();
...
Some of the targets use this constant, some don't. So I get:
error: 'core::SZ_MAX' defined but not used"
I use scons with GCC 4.7.3 on Linux. I have -Wall set and want to keep it that way.
As far as I understand from the GCC documentation, this shouldn't give a warning:
-Wunused-variable
Warn whenever a local variable or non-constant static variable is unused aside from its declaration. This warning is enabled by -Wall.
So I don't see why I get a warning (which turns into an error).
On other answers, people were advised to make the declaration extern and to do the assignment in the file that uses the constant. This file is used by many other files, so it would loose its constant-ness if I do that. Furthermore, this file has header guards, so I think this should mean that the constant is actually created only once.
I'd appreciate any help!
Yuval
Possible duplicates:
How to use typed constants with “unused variable” warnings?
c++ static array declared in h file gives warning 'defined but not used'
It seems that this was not the error that halted compilation.
Rather, if GCC find another error, it would still report on this too.
I actually had another unused variable, and that's what caused this error in the first place.
For example, when creating the following files:
file1.cc
#include "head1.hh"
int main() {
int bad_unused_variable;
return my_ns::JUST_ANOTHER_CONST;
}
head1.hh
#ifndef HEAD1
#define HEAD1
#include <stdint.h>
#include <cstddef>
#include <limits>
namespace my_ns {
typedef std::size_t Size;
static const Size SZ_MAX = std::numeric_limits<Size>::max();
static const Size JUST_ANOTHER_CONST = 8;
}
#endif
You get:
> g++ -Wall -Werror file1.cc -O2 -std=c++98 -o file1
file1.cc: In function 'int main()':
file1.cc:4:6: error: unused variable 'bad_unused_variable' [-Werror=unused-variable]
In file included from file1.cc:1:0:
head1.hh: At global scope:
head1.hh:10:20: error: 'my_ns::SZ_MAX' defined but not used [-Werror=unused-variable]
cc1plus: all warnings being treated as errors
EDIT
This also seems to have been answered here: gcc warnings: defined but not used vs unused variable - there they mention the subtle differences between the two warning messages (unused variable vs defined but not used). Still, it doesn't really answer as to why GCC behaves this way...

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.

How to use const auto with STLport library

I've tried to compile elementary example:
#include <vector>
int main ()
{
std::vector<int> testV;
for (const auto& test : testV)
{ }
return 0;
}
And I've received error:
test.cpp: In function 'int main()':
test.cpp:5:29: error: 'begin' was not declared in this scope
test.cpp:5:29: error: 'end' was not declared in this scope
test.cpp:5:29: error: unable to deduce 'const auto&' from '<expression error>'
Does STLport support const auto ?
EDIT: I'm using GCC 4.6
With 4.7 and more everything is ok.
gcc 4.6 came out in the spring of 2011, was not without bugs in most C++11 features. Moroever, around the same time the rules for ADL lookup in range-for were also modified (note that this was prior to the official ratification of the C++11 Standard in the summer of 2011). See this Q&A for more details. It's probably not worth debugging this and the recommended course of action is to upgrade to a recent version of gcc (4.7 or preferably 4.8).