I wanted to play around with the new features of C++11, namely with Smart Pointers. I found an exampleg++ (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2 here: Cplusplus11-Smart-Pointers:
#include <memory>
int main() {
std::shared_ptr<int> sptr1( new int );
}
When I try it out is everything I get:
In function ‘int main()’:|
error: ‘shared_ptr’ was not declared in this scope|
error: expected primary-expression before ‘int’|
error: expected ‘;’ before ‘int’|
This is my g++ Version:
g++ (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2
Do I have to "activate" C++11 at first?
You need to pass the -std=c++11 compiler flag to the g++ compiler. CodeBlocks allows you to configure this via
Settings -> Compiler -> Compiler Settings
Note for older versions of gcc, you may need -std=c++0x.
Related
im trying to build the example here https://github.com/jtv/libpqxx#programming-with-libpqxx using c++ compiler on my beagleboneblack, ececuting the command: g++ -o taks tasks.cpp -lpqxx -lpq
I belive i have installed the lpqxx library with success.
Gcc compiler version: gcc (Debian 8.3.0-6) 8.3.0
When compiling i get the following errors (and several other not show here):
warning: structured bindings only available with -std=c++17 or -std=gnu++17
for (auto [name, salary] : tx.query<std::string, int>(
^
tasks.cpp:20:43: error: ‘using work = class pqxx::transaction<>’ {aka ‘class pqxx::transaction<>’} has no member named ‘query’
for (auto [name, salary] : tx.query<std::string, int>(
^~~~~
tasks.cpp:20:60: error: expected primary-expression before ‘,’ token
The first is a warning, but the others error. Not familiar with C#:
What does these errors mean?
How do i get on solving them?
Is it compiler issues or is it code issues?
I just have one file in my direcory and that is copy-paste of the example from the link, am i missing some header files that define some stuff for me?
I was trying to compile this code (in the file test.cpp)
#include<tuple>
int main(){
auto [c,d] = make_tuple(3.1,2.3);
}
using
g++ -std=c++17 test.cpp -o test
, as well as
clang++ -std=c++1z test.cpp -o test
both would print the error message:
test.cpp: In function ‘int main()’:
test.cpp:3:7: error: expected unqualified-id before ‘[’ token
auto [c,d] = make_tuple(3.1,2.3);
using
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609
and
clang version 3.8.0-2ubuntu4
(using Ubuntu 16.04.09)
What am I missing?
From the official website of GCC:
Structured bindings is supported in gcc version 7
From the official website of clang:
Structured bindings is supported in version 4.
The following link presents compiler support for new C++ features. https://en.cppreference.com/w/cpp/compiler_support
clang++ main.cpp -std=c++11 -target x86_64-unknown-win32 -I/usr/lib/gcc/x86_64-w64-mingw32/4.6/include/
main.cpp:6:10: fatal error: 'string' file not found
#include <string>
^
1 error generated.
that's weird because:
/usr/lib/gcc/x86_64-w64-mingw32/4.6/include/ssp$ ls
ssp.h stdio.h string.h unistd.h
and this result the same:
clang++ main.cpp -std=c++11 -target x86_64-unknown-win32 -I/usr/lib/gcc/x86_64-w64-mingw32/4.6/include/ssp
well. I used to compile c code from linux to windows just fine using:
x86_64-w64-mingw32-gcc main.c
and I tried:
x86_64-w64-mingw32-g++ -std=c++0x main.cpp
also:
x86_64-w64-mingw32-g++ -std=c++0x main.cpp -I/usr/lib/gcc/x86_64-w64-mingw32/4.6/include
still give bunch of errors like
main.cpp:70:23: sorry, unimplemented: non-static data member initializers
main.cpp:70:23: error: in-class initialization of static data member ‘origin’ of non-literal type
main.cpp: In constructor ‘Item::Item(std::string)’:
main.cpp:83:18: error: ‘stoul’ is not a member of ‘std’
main.cpp:88:4: error: ‘origin’ was not declared in this scope
main.cpp:89:17: error: ‘stof’ is not a member of ‘std’
main.cpp: In function ‘std::ostream& operator<<(std::ostream&, const Item&)’:
....
x86_64-w64-mingw32-g++ (GCC) 4.6.3 seems to be the latest version on ubuntu repo.
I'm at lost..why the hell clang won't use the headers. why the hell x86_64-w64-mingw32-g++ won't listen that it should use c++11 standards.
The GCC version you use does not support non-static data member initializers as shown here. Find out here which version of GCC supports the C++11 feature set you required.
I'd like to try the new Hinnant's short_alloc allocator that, as far as I can understand, replaces the old stack_alloc allocator. However, I can't manage to compile the vector example. g++ says:
~# g++ -std=c++11 stack-allocator-test.cpp -o stack-allocator-test
In file included from stack-allocator-test.cpp:6:0:
short_alloc.h:11:13: error: ‘alignment’ is not a type
short_alloc.h:11:22: error: ISO C++ forbids declaration of ‘alignas’ with no type [-fpermissive]
short_alloc.h:11:22: error: expected ‘;’ at end of member declaration
As far as I can tell, g++ complains about line 10 and 11:
static const std::size_t alignment = 16;
alignas(alignment) char buf_[N];
It seems that the compiler doesn't like the "expression version" of alignas but it expects just the "type-id version".
I'm using g++ 4.7.2 under Ubuntu 12.10.
~# g++ --version
g++ (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2
Probably I'm missing something obvious, but I can't figure it out. Any help would be appreciated. (Please don't tell me I have to upgrade to a newer g++, I'm too lazy to do that :)
g++-4.7.2 doesn't support alignas. From http://gcc.gnu.org/projects/cxx0x.html:
Alignment support | N2341 | GCC 4.8
Try using g++-4.8.0 or clang; alternatively you may be able to use the __attribute__((aligned)):
__attribute__((aligned (8))) char buf_[12];
Note that __attribute__((aligned)) only accepts certain integer constant expressions (literals, template parameters); it doesn't accept static const variables.
When trying to compile a simple class (g++ myclass.cpp), I get the following error:
ISO C++ forbids declaration of ‘tuple’ with no type
I searched for this problem, and in most cases people seemed to forget std:: or including <tuple> in the header. But I have both. Here is my code:
myclass.h
#ifndef MYCLASS
#define MYCLASS
#include <iostream>
#include <tuple>
class MyClass {
std::tuple<bool, int, int> my_method();
};
#endif
myclass.cpp
#include "myclass.h"
using namespace std;
tuple<bool, int, int> MyClass::my_method() {
return make_tuple(true, 1, 1);
}
If I do the same using pair instead, leaving out the second int and including <set>, it works.
What am I missing?
EDIT:
Here is the full output:
$ g++ myclass.cpp -o prog
In file included from myclass.cpp:1:
myclass.h:7: error: ISO C++ forbids declaration of ‘tuple’ with no type
myclass.h:7: error: invalid use of ‘::’
myclass.h:7: error: expected ‘;’ before ‘<’ token
myclass.cpp:5: error: expected constructor, destructor, or type conversion
before ‘<’ token
$ g++ --version
i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658)
(LLVM build 2336.11.00)
GCC 4.2.1 shipped with every mac is outdated. It will not recognize the C++11.
You need to compile your code using: c++ instead of g++ which calls clang, which is the officially updated compiler on mac.
c++ -std=c++11 -stdlib=libc++ myclass.cpp -o prog
You are required to link against libc++ which is clang lib which knows about c++11 features instead of the default libstdc++ used by gcc.
Update! We're on GCC 4.7 these days.
GCC 4.2.1 is from all the way back on 18th July, 2007. There is only a remote chance that it supports any features from what became C++11.
That said, it may provide some in std::tr1 (i.e. std::tr1::tuple<T1, T2, ...>), which is where some of the C++11 features lived in the time before standardisation, though off the top of my head these were introduced to GCC only in 4.4.
With gcc 4.2, tuple was in namespace std::tr1. You must include <tr1/tuple> and specify your method more or less like this
#ifndef MYCLASS
#define MYCLASS
#include <tr1/tuple>
class MyClass {
std::tr1::tuple<bool, int, int> my_method();
};
#endif
Although, as others already suggested, updating to a more recent gcc might be more appropriate.
If you add the -std=c++11 (or, for older versions of g++ the -std=c++0x) option and add a simicolon after the expression in the member function the code compiles. If this doesn't work you might have a version which only defines tuple in namespace std::tr1 (it seems, the implementation provides a <tuple> header, though, because there is no error about <tuple> not being found).