Compile error using boost::concept_check - c++

I'm trying to compile simple example to use the boost concept_check
Code is as follow:
#include <vector>
#include <complex>
#include <algorithm>
#include <boost/iterator.hpp>
#include <boost/concept_check.hpp>
template <class foo>
void my_do_sort(std::vector<foo>& v)
{
BOOST_CONCEPT_ASSERT((RandomAccessIterator<foo>));
std::stable_sort(v.begin(),v.end())
}
int main()
{
std::vector<std::complex<double> > v;
v.push_back(std::complex<double>(1,3));
v.push_back(std::complex<double>(2,4));
my_do_sort(v);
}
I then get the following error:
g++ -I~/tmp/BOOST/boost_1_39_0 -g3 -ggdb -pedantic -pedantic-errors -Wall -Werror -O0 --save-temps con1.cpp -o con1
con1.cpp: In function 'void my_do_sort(std::vector<foo, std::allocator<_CharT> >&)':
con1.cpp:11: error: `*' cannot appear in a constant-expression
con1.cpp:11: error: a call to a constructor cannot appear in a constant-expression
con1.cpp:11: error: template argument 1 is invalid
con1.cpp:11: error: template argument 1 is invalid
con1.cpp:11: error: invalid type in declaration before ';' token
make: *** [con1] Error 1
Thanks

If you re-read your code, this shouldn't be surprising. It fails to compile because the concept check fails. You are asserting that foo should implement the RandomAccessIterator concept. The entire point in the library is to produce a compile error (just like the one you're seeing) if the concept check fails.
But foo is not an iterator. it is a std::complex<double>.
It should be BOOST_CONCEPT_ASSERT((RandomAccessIterator<v::iterator>)); as far as I can see.
You want to check that the vector iterator is a random access iterator. Not that the complex numbers stored in the iterator are random access iterators.

This was just compilation issue. I had to use boost namespace.

Related

Compilation error: `error: definition of implicitly-declared`

I am trying to create a class which calls one of it's functions when created, but I am getting the following error when compiling:
g++ -std=c++11 -Wall -Wextra -Werror -pedantic-errors -DNDEBUG -c src/PuzzleSolution.cpp
src/PuzzleSolution.cpp:7:32: error: definition of implicitly-declared 'PuzzleSolution::PuzzleSolution()'
PuzzleSolution::PuzzleSolution()
^
src/PuzzleSolution.cpp:12:6: error: prototype for 'void PuzzleSolution::addRow()' does not match any in class 'PuzzleSolution'
void PuzzleSolution::addRow()
^
src/PuzzleSolution.h:19:10: error: candidate is: void PuzzleSolution::addRow(std::vector<unsigned int>&)
explicit PuzzleSolution();
^
src/PuzzleSolution.cpp:17:48: error: no 'void PuzzleSolution::addElement(unsigned int)' member function declared in class 'PuzzleSolution'
void PuzzleSolution::addElement(unsigned int id)
^
make: *** [PuzzleSolution.o] Error 1
Here is the header:
#include <vector>
using namespace std;
class PuzzleSolution {
private:
vector<vector<unsigned int>> sol;
public:
explicit PuzzleSolution();
void addRow();
};
Here is the cpp file:
#include "PuzzleSolution.h"
PuzzleSolution::PuzzleSolution()
{
addRow();
}
void PuzzleSolution::addRow()
{
this->sol.emplace_back();
}
What am I doing wrong?
The code as it is has no error. It compiles with GCC 4.8.2
Be sure that your header file is indeed what you have linked to. Most likely the header being included is different than the one you have actually posted here.
Side Note: Generally it is considered as a bad practice to put using namespace std; in a header file.
Found the issue:
There was a file in the src folder called PuzzleSolution.h.gch
#Quatin and #StoryTeller helped me to understand that this is a pre-compiled header, which the compiler kept using.
Once deleted, the project compiled and executed

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.

Rcpp: error occured building shared library

I have a cpp code in a file named PSM.cpp as given below,
#include <Rcpp.h>
// [[Rcpp::export]]
std::vector<std::string> useInitLists() {
std::vector<std::string> vec = {"larry", "curly", "moe"};
return vec;
}
When I source the following code, I get following error.
> Rcpp::sourceCpp("PSM.cpp")
g++ -m64 -I"C:/PROGRA~1/R/R-30~1.2/include" -DNDEBUG -I"C:/Users/30708/Documents/R/win-library/3.0/Rcpp/include" -I"d:/RCompile/CRANpkg/extralibs64/local/include" -O2 -Wall -mtune=core2 -c PSM.cpp -o PSM.o
PSM.cpp: In function 'std::vector<std::basic_string<char> > useInitLists()':
PSM.cpp:5:60: error: in C++98 'vec' must be initialized by constructor, not by '{...}'
PSM.cpp:5:60: error: could not convert '{"larry", "curly", "moe"}' from '<brace-enclosed initializer list>' to 'std::vector<std::basic_string<char> >'
make: *** [PSM.o] Error 1
Error in Rcpp::sourceCpp("PSM.cpp") :
Error 1 occurred building shared library.
Can someone please suggest what the error means and how to resolve the same? Thanks.
You can use the alternative -std=c++0x, but I never used g++ on Windows.
(Glad it helped !)

Compilation error when templating my class on coordinate type and using Boost Geometry library

I'm writing a library code on top of Boost Geometry library. My class should be templated on the coordinate type (usually int/float/double etc.).
The code below (stripped down to bare minimum) doesn't compile and I get a compilation error that doesn't help me.
The code:
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point.hpp>
template <typename CoordType>
class MyClass {
public:
typedef boost::geometry::model::point<CoordType, 2, boost::geometry::cs::cartesian> MyPoint;
CoordType getX(const MyClass<CoordType>::MyPoint &p) const { return p.get<0>(); }
};
The error:
test.cpp: In member function 'CoordType MyClass<CoordType>::getX(const MyClass<CoordType>::MyPoint&) const':
test.cpp:8:82: error: expected primary-expression before ')' token
I'm compiling this code with: g++ -I./boost_1_54_0 test.cpp -o test.o. I used different versions of G++ 4.5.2/4.7.2/4.8.1, but I still get the same error.
What am I missing here?
Thanks in advance.
Using the free function boost::geometry::get<0>(p); recommended in the boost docs circumvents this problem.
I agree with the answer of us2012, using boost::geometry::get<0>() is recommended.
The actual problem was that the template keyword was missing, so this:
{ return p.template get<0>(); }
would have fixed the problem.

Can't build sigqueue example with gcc but g++ is ok?

I have a strange build problem.
I have a simple test program that sends a sigqueue to another process.
This little code example builds and runs when I build it as a c++ program (compiled with g++)
but when I compile it as a c program (with gcc) I get a error that he can't find the sigval struct.
The short example:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
int main(int argc, char *argv[])
{
sigval value;
value.sival_int = 123;
sigqueue(0,SIGUSR1, value);
}
Please note that I replaced the pid with 0 to simplify this question.
And if I compile with gcc I get this:
$> gcc sigusr1_mini.c
sigusr1_mini.c: In function ‘main’:
sigusr1_mini.c:9: error: ‘sigval’ undeclared (first use in this function)
sigusr1_mini.c:9: error: (Each undeclared identifier is reported only once
sigusr1_mini.c:9: error: for each function it appears in.)
sigusr1_mini.c:9: error: expected ‘;’ before ‘value’
sigusr1_mini.c:10: error: ‘value’ undeclared (first use in this function)
What am I missing here, why can't he find the sigval struct?
And why can g++ find it?
Thanks
Johan
In C, struct and union tags do not introduce names that can be used on their own like they do in C++. You must spell it out:
union sigval value;
How is sigval defined in h-file? C compiler may require full definition, for example:
union sigval value;