Archive for the 'c++' Category

Stupid compiler error messages

Posted by mikedaum on January 27th, 2009

Just got this delightful error message:

gslwrap/HMCMC.cpp:57: error: conversion from ‘boost::cb_details::iterator<boost::circular_buffer<bool, std::allocator<bool> >, boost::cb_details::const_traits<std::allocator<bool> > >’ to non-scalar type ‘boost::cb_details::iterator<boost::circular_buffer<bool, std::allocator<bool> >, boost::cb_details::nonconst_traits<std::allocator<bool> > >’ requested

I get it and its ilk fairly regularly.  Takes me a bit to actually sort it out.  Don’t know why.  What it’s trying to say is that I used a non-const iterator somewhere where it needed to be a const_iterator.

Was that so hard?

Fussy C++

Posted by mikedaum on June 11th, 2008

So last night I’m all

GslMatrix D;
GslMatrix B;
C.symmetricEigs(B, D.diag() );


But the compiler is all

error: no matching function for call to 'GslMatrix::symmetricEigs(GslMatrix&, GslVector)


See the problem? D.diag() is passed in as a temporary, and the symmetricEigs has it passed in as a reference. Rather than behave rationally, the compiler refuses to consider the possibility that I meant it to pass the temporary by reference, and tells me that it can’t find a proto for what I want to do.

Now I ask you: is this good behaviour on the compiler’s part? Is this what I wanted to do? A nice error telling me that I’m trying to pass a temporary by reference would have been a little better. But is it really an error? What about side effects? I was certainly counting on those in this case, but it refused to play ball.

Anyway, I’m leaning towards my not understanding the situation. Maybe there’s really a technical or spec reason why this error is necessary. For the life of me, I can’t think of one though. So until one of you points out the essential thing that I”m missing, I’m going with this should be a warning at best, but should certainly be allowed.

Oh, and btw, the solution was to create

GslVector d;

one line above and pass that in instead. On the stack like the temporary, but somehow able to give compiler that nice cozy feeling it needed.