I tried to get myself into C++ and purchased the book "Programming - Principles and Practice Using C++" by Bjarne Stroustrup.
When I tried to get compile the following source code:
#include "std_lib_facilities.h"
int main(){
cout<<"Hello, World!\n";
keep_window_open();
return 0;
}
I am getting following compile error:
In file included from /Users/hypertrooper/Documents/Programming - Principles and Practice Using C++/hello_world.cpp:1:
std_lib_facilities.h:71:20: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using size_type = typename std::vector<T>::size_type;
^
std_lib_facilities.h:102:20: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using size_type = std::string::size_type;
^
std_lib_facilities.h:107:8: warning: comparison of unsigned expression < 0 is always false [-Wtautological-compare]
if (i<0||size()<=i) throw Range_error(i);
~^~
std_lib_facilities.h:113:8: warning: comparison of unsigned expression < 0 is always false [-Wtautological-compare]
if (i<0||size()<=i) throw Range_error(i);
~^~
std_lib_facilities.h:213:107: error: expected '(' for function-style cast or type construction
inline int randint(int min, int max) { static default_random_engine ran; return uniform_int_distribution<>{min, max}(ran); }
~~~~~~~~~~~~~~~~~~~~~~~~~~^
std_lib_facilities.h:222:20: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using Value_type = typename C::value_type;
^
std_lib_facilities.h:225:18: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using Iterator = typename C::iterator;
^
6 warnings and 1 error generated.
I get it that my compiler is not using C++11 feature, but I do not know how I can update the compiler. I should let know that I am using a MacOSX (10.10 Yosemite) and tried to compile with xCode and textmate. I even tried to follow this tutorial(https://wiki.helsinki.fi/display/HUGG/Installing+the+GNU+compilers+on+Mac+OS+X), but it did not helped. (At least when I tried to compile with text mate)
I hope you are able to help me. :(
If you are on a Mac or Linux, the compiler is usually g++ or clang; to access C++11, just specify -std=c++11 as an option when invoking the compiler (Assuming that you have an up-to-date version).
What you need to do is open the project settings -> Build Settings and set C++ Language Dialect to C++11 and the C++ Standard Library to libc++ (LLVM C++ standard library with C++11 support).
From your build settings, adjust the C++ Language Dialect to C++11...
Also make sure you are using the LLVM C++ library, as it has full C++ 11 support.
Related
I attempting to use a nested lambda as follows:
int main() {
auto x=[](int a){
return [a](int b){
return a+b;
};
};
int xx = x(1)(2);
(void)xx;
return 0;
}
However, the vs2013 compiler yields an error (C++11 language level):
[...] can't convert to int.
What is wrong?
Your code snippet is well-formed, and this is a compiler limitation or bug in the MSVC C++ compiler version used in VS2013. Note that VS2013 only claimed partial C++11 support, as per e.g. What's New for Visual C++ in Visual Studio 2013:
Improved ISO C/C++ Standards Support
Compiler
Supports these ISO C++11 language features:
[...]
I am compiling the code on solaris 5.11.
G++ version is 4.8.2.
The same code works on Ubuntu but gives the error: 'to_string() was not declared in this scope' on solaris.
I went through many links and tried the following things:
Adding 'std::' before to_string(). This gives error - 'to_string is not a member of std'
Added 'std=c++11' or 'std=c++0x' while compilation.
Both the above things do not work.
Is there anything related to Solaris?
The actual code was very huge. So simulating the error in sample code below.
temp.cpp
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
int i = 10;
str = "john age is " + to_string(i);
cout << str;
return 0;
}
command: g++ temp.cpp -std=c++0x -o temp
For GCC 4.8.2 the to_string functions are defined conditionally, according to the following:
#if ((__cplusplus >= 201103L) && defined(_GLIBCXX_USE_C99) \
&& !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF))
The GLIBCXX_USE_C99 macro depends on a large number of C99 functions being supported by the OS, so presumably the necessary C99 library functions were not found when building GCC on Solaris. So the to_string definitions are absent.
In current versions of GCC the condition is more fine-grained, and checks whether the C99 functions are defined in C++98 mode and C++11, so that the absence of any C99 function doesn't disable everything:
#if __cplusplus >= 201103L
//...
#if _GLIBCXX_USE_C99_STDIO
It's not possible to backport these improvements to GCC 4.8, so you might need to update to at least GCC 6.
compile using std=c++11 as below
g++ -std=c++11 filename.cc
Note : your compiler must support c++11
I am trying to compile some C++ code (which can be compiled with Visual Studio 2012 on Windows) with g++-4.4.
I have this snippet of code,
const std::string cnw::restoreSession(const std::vector<string> &inNwsFile) {
for (std::string &nwFile : inNwsFile){
// some...
}
}
that I cannot compile because of this error:
CNWController.cpp:154: error: expected initializer before ‘:’ token
Can you give me some advise on how to solve this problem?
Your compiler is too old to support range-based for syntax. According to GNU it was first supported in GCC 4.6. GCC also requires you to explicitly request C++11 support, by giving the command-line option -std=c++11, or c++0x on compilers as old as yours.
If you can't upgrade, then you'll need the old-school equivalent:
for (auto it = inNwsFile.begin(); it != inNwsFile.end(); ++it) {
std::string const &nwFile = *it; // const needed because inNwsFile is const
//some...
}
I believe auto is available in GCC 4.4 (as long as you enable C++0x support), to save you writing std::vector<string>::const_iterator.
If you really do need a non-const reference to the vector's elements then, whichever style of loop you use, you'll need to remove the const from the function parameter.
I'm having trouble working with lambda functions in the Intel compiler, in particular, the following code won't compile:
template<typename T>
std::function<T (int)> make_func(T x) {
return [=](int index) -> T
{
return x;
};
}
The error I get is
error: namespace "std" has no member "function"
The code compiles and runs fine on my Mac, (macports gcc version 4.5). The error is at work, where we use the Intel compiler version 11.1. It does accept lambda functions (with the -std=c++0x option), such as:
auto lam = [=](int j) -> int {
printf("testing for lambdas: %d\t%d\n", n, j);
return n;
};
int g = lam(7);
The version of gcc installed at work is 4.1.2, so I'm guessing that the standard library is old?
/bin/libc.so.6
says it's version 2.5 compiled with gcc 4.1.2.
Is there a way around this?
thanks in advance for any help
I get the same behavior with icc 11.1 on a system where gcc 4.5.2 is installed.
g++'s header <functional> is protected with #ifdef __GXX_EXPERIMENTAL_CXX0X__ which is not defined when icc is used.
I would consider switching to boost::function in this setup, which of course works with icc.
Well, the code shown doesn't include a single header. And yet you refer to the standard library std::function.
So no, it doesn't compile. As with any other part of the standard library, you need to include the header where std::function is defined: <functional>.
In the following example:
void foo (double *ptr)
{
const double * restrict const restr_ptr=ptr;
}
I get this error:
error: expected a ";" const double * restrict const restr_ptr=ptr;
^
I compile with -std=c99, using gcc 3.4
Any Ideas?
In C++, restrict is not a keyword (except for Microsoft extensions). It doesn't mean what it does in C. It looks as though you tried to apply C99 mode to your C++ compiler. Use a C compiler to compile C code, and use a C++ compiler to compile C++. Neither language is a subset of the other.