boost accumulators example doesn't compile - c++

I installed boost (1.60.0) on Linux Mint 17.3 Rosa and tried to compile the boost accumulator example (http://www.boost.org/doc/libs/1_60_0/doc/html/accumulators/user_s_guide.html) with the gcc compiler (v 4.8.4 64 bit) using this command:
>g++ -o exaccu exaccumulator.cpp -I/usr/local/lib/boost_1_60_0/
Compilation failed with a long list of error messages starting with:
>exaccumulator.cpp: In function ‘int main()’:
>exaccumulator.cpp:22:32: error: ‘accumulators’ has not been declared
>std::cout << "Moment: " << accumulators::moment<2>(acc) << std::endl;
After looking up accumulators.hpp I changed accumulators::moment<2> to moment<2>. This did the trick and the compilation (with the same flags) succeeded. Alternatively, prefixing "accumulators" with "boost::accumulators::moment<2>" worked too. So my question is: Is there something wrong with my installation of boost or is there a typo in the example from the tutorial ?

This seems to be a typo indeed.
You can use the whole boost namespace (bad idea) to make the example compile:
using namespace boost;
accumulators::moment<2>(acc);
Or, like you already did, just remove the accumulators:: specificier and only use namespace boost::accumulators;.
Or just specify it's fully qualified name : boost::accumulators::moment<2>(acc).

Related

gcc compile error with sine and boost:interval

I'm required to run some code on a linux (CentOs 6.7) server with gcc/g++ installed (7.2.0) and boost (1.66) (Edit: not only 1.66, an older version of boost interfered. Keep your machines clean guys). I specified my interval type with policies like so:
// test.cpp
#include <boost/numeric/interval.hpp>
namespace bn = boost::numeric;
namespace bi = bn::interval_lib;
using Interval = bn::interval<
double,
bi::policies<
bi::save_state<bi::rounded_transc_std<double> >,
bi::checking_base<double>
>
>;
Now I want to do some simple calculation like:
// still test.cpp
int main()
{
Interval iv_arg {1.0};
Interval res = sin(iv_arg);
}
On my local machine, a mac, compiling with clang works perfectly fine. However, as soon as I try to run it on the server, compiling with g++ -std=c++11 test.cpp I'm getting the error:
/usr/include/boost/numeric/interval/rounded_arith.hpp:71:59: error:
'to_int' was not declared in this scope, and no declarations were
found by argument-dependent lookup at the point of instantiation
[-fpermissive] T int_down(const T& x) { this->downward(); return
to_int(x); }
~~~~~~^~~ /usr/include/boost/numeric/interval/rounded_arith.hpp:71:59: note:
declarations in dependent base
'boost::numeric::interval_lib::detail::c99_rounding' are not found by
unqualified lookup
/usr/include/boost/numeric/interval/rounded_arith.hpp:71:59: note: use
'this->to_int' instead
There is some more info which seems irrelevant to the topic, but I can include it if it might help. I tried to google a little but couldn't find anything relevant. If possible, I would like to find a solution that doesn't require any changes on the server. Does anyone have an idea or encountered a similar problem before?
First version of boost that compiles this is 1.58
boost 1.57 ERROR
boost 1.58 OK

C++ error: invalid use of ‘::’ for `std::cout`

This is probably the weirdest thing I have seen in the last few years.
I have a project that builds perfectly well in two completely different machines (openSUSE Tumbleweed and ubuntu 14.04).
I started with a new machine using kubuntu 16.04 and this error started happening:
$ g++ -std=c++14 cout_qualif.cpp -lpng -o cout_qualif
In file included from cout_qualif.cpp:1:0:
debug_utils.h:19:19: error: invalid use of ‘::’
# define msg std::cout
Clang also points an error, but with a quite different message:
$ clang -std=c++14 cout_qualif.cpp -lpng -o cout_qualif
In file included from cout_qualif.cpp:3:
In file included from /usr/include/png++/png.hpp:34:
In file included from /usr/include/png.h:317:
/usr/include/zlib.h:94:19: error: non-friend class member 'cout' cannot have a qualified name
z_const char *msg; /* last error message, NULL if no error */
^~~
./debug_utils.h:19:19: note: expanded from macro 'msg'
# define msg std::cout
~~~~~^
1 error generated.
The simplest test code I've came across is:
#include <iostream>
#include "debug_utils.h"
#include <png++/png.hpp>
int main()
{
msg << "Start" << std::endl;
png::image< png::rgb_pixel > image("input.png");
image.write("output.png");
msg << "Finish" << std::endl;
return 0;
}
And "debug_utils.h":
#ifndef DEBUG_UTILS_H
#define DEBUG_UTILS_H
#include <iostream>
# define msg std::cout
#endif // DEBUG_UTILS_H
It turns out that "png.h" includes "zlib.h" and there is defined a struct:
typedef struct z_stream_s {
// ...
z_const char *msg; /* last error message, NULL if no error */
This msg member is what is triggering the error. If I move my #include "debug_utils.h" one line below, after #include <png++/png.hpp>, everything seems to work fine.
Now finally the question:
Why this machine cannot compile my code while two other can?
Additional info:
Kubuntu 16.04:
$ g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
$ clang --version
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
openSUSE Tumbleweed:
g++ is 7.1.1
Ubuntu 14.04:
Exact version not available at hand but I believe it is 4.9.x
To recap the cause of the problem
# define msg std::cout
in debug_utils.h substitutes std::cout for any instances of msg throughout the code after debug_utils.h is included. As msg is a common short identifier, particularly for a message buffer, an unintended substitution has always been a risk lurking in the code. The solution to this is obvious: Don't do that. Use a longer, less likely to be duplicated substitution or don't do it at all and replace the macro. Without seeing your usecase I'd probably replace the macro with a function that returns the correct stream and the compiler can easily inline.
The confusion and the question stems from why an error from the macro substitution only arises from a simple piece of test code when compiled on only one of three candidate PCs.
The answer is differences in the toolchain and support libraries. For one reason or another, on only one of these PCs a third-party header that uses the msg identifier is included by a third-party header included by the test program. The other two follow different inclusion paths to build the same program and avoid tripping over the unwanted substitution.

Clang fails to find iostream. What should I do?

Earlier, I posed a related question.
I have the following program extracted from a large project in my Mac OS
#include <iostream>
int main(){
std::cout<<"hello"<<std::endl;
return 0;
}
Compiling it with Clang fails with the following error:
$ clang test.cpp
test.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
^
1 error generated.
For information,
A) I have already installed xcode command line tools, using xcodeselect --install. But it seems iostream does not locate in the default search path of clang.
B) Using g++ instead of clang compiles the program. But in my problem, I am not allowed to use other compiler than clang, or to change the source program.
C) I can see workaround techniques, e.g, by tweaking the search path in .bashrc or with some symbolic link, etc. But I feel reluctant to use them, because it seems that I have an installation problem with my Clang and tweaking the path only helps to avoid one of these path issues.
clang and clang++ do different things. If you want to compile C++ code, you need to use clang++
Alternatively you can invoke c++ compiler directly by providing language name explicitely:
clang -x=c++

minGW (with g++ 4.7.1) not compiling simple C++11 code (under WinVista)

I did my homework and searched for an answer here and on the net. The simple code below is not compiling:
#include <thread>
#include <iostream>
void hello()
{
std::cout << "Hello from thread " << std::endl;
}
int main()
{
std::thread t1(hello);
t1.join();
return 0;
}
It's very simple code, but I'm getting the following errors:
Thread_Cpp11_002.cpp: In function 'int main()'
Thread_Cpp11_002.cpp:14:5: error: 'thread' is not a member of 'std'
Thread_Cpp11_002.cpp:14:17: error: expected ';' before 't1'
Thread_Cpp11_002.cpp:15:5: error: 't1' was not declared in this scope
And I've tried a lot of things, none of which have worked:
Declaring t1 using thread instead of std::thread
Compiling with g++ 4.8.0
Using the following flags, individually and together: -pthread, -std=gnu++11, -std=c++0x, -std=c++11...
So, I think this question it worth being posted here.
These compilation were done through the command line. After I have it working I will try with Code::Blocks 12.11, which worked fine for C++98.
Remember, I'm using Windows Vista.
This is a well-known issue with some builds of MinGW (just search for mingw thread in your favorite engine). If you need thread support, you have to use another library (such as boost or the native Windows API) or a different build.

getting started with boost cpu_timer, error: ‘boost::timer::cpu_timer’ has not been declared

I'm trying to compile this simple program to start learning how to use timers:
#include <boost/timer.hpp>
using boost::timer::cpu_timer;
//...
nanosecond_type last_checkpoint_time = 0;
cpu_timer checkpoint_timer; // start the timer
for (;;)
{
//process_a_transaction();
if (checkpoint_timer.elapsed().user - last_checkpoint_time > 5*1000000000LL)
{
//... create a checkpoint ...
last_checkpoint_time = checkpoint_timer.elapsed().user;
cout << checkpoint_timer.elapsed().user << endl;
}
}
I'm using gentoo linux and issuing the following command to compile:
g++ -I /usr/include/boost-1_46 timer1.cpp -o timer
I get these errors:
timer1.cpp:3:21: error: ‘boost::timer::cpu_timer’ has not been declared
timer1.cpp:5:1: error: ‘nanosecond_type’ does not name a type
timer1.cpp:6:1: error: ‘cpu_timer’ does not name a type
timer1.cpp:8:1: error: expected unqualified-id before ‘for’
timer1.cpp:8:8: error: expected unqualified-id before ‘)’ token
I was reading the docs under errors and warnings but the problem I am having is that I only have two libraries:
/usr/lib/libboost_test_exec_monitor-1_46.a
/usr/lib/libboost_test_exec_monitor-mt-1_46.a
Is this because I did not use the static-libs flag during compile of boost? Would I be better off using static-libs? Maybe this is a tangent. What else can cause the errors given above? Please forgive my ignorance as I am pretty new to C++/boost.
Thanks
I haven't been using cpu_timer my self, but a quick Google search seems to indicate you should include <boost/timer/timer.hpp> instead. As for the error of nanosecond_type you need to use another using statement for that.
I think I figured out what the problem is. I was quoting an example in my original post from the ver 1.49 documentation. cpu_timer was first discussed in the boost documentation in ver 1.48. The stable version on gentoo is currently 1.46 and testing only provides ver 1.47, ver 1.48 is hardmasked. So my only option is to remove boost from my system download the tar of 1.49 and possibly break my system wrt boost or wait for the hardmask to be removed from ver 1.48.
In any case, static-libs is certainly irrelevant because this is a compiler error, not a linker error. It doesn't look at the libraries until the linker stage, until then only the headers are relevant.