I'm attempting to follow this example from the documentation (see typedef for word_counter).
#include <string>
#include <boost/bimap.hpp>
#include <boost/bimap/unordered_set_of.hpp>
typedef boost::bimap
<
boost::bimap::unordered_set_of< std::string >,
std::string
> MyBimap;
Error thrown is
test.cpp:11:1: error: wrong number of template arguments (1, should be 5)
In file included from /usr/include/boost/bimap.hpp:13:0, from test.cpp:3:
/usr/include/boost/bimap/bimap.hpp:133:7: error: provided for ‘template class boost::bimaps::bimap’
test.cpp:11:10: error: invalid type in declaration before ‘;’ token
You have a typo.
Instead of
boost::bimap::unordered_set_of< std::string >,
use
boost::bimaps::unordered_set_of< std::string >,
in the template.
It will compile then.
Related
The following simple code
#include <string>
#include <sstream>
#include <memory>
#include <tuple>
#include <map>
#include <cassert>
struct foo {
foo(std::ostringstream&, std::unique_ptr<int>&&);
};
struct bar {
std::map<std::string,foo> map;
void add(std::string const &name,
std::ostringstream &str,
std::unique_ptr<int> &&ptr)
{
auto result = map.emplace
( std::piecewise_construct,
std::forward_as_tuple(name),
std::forward_as_tuple(str,std::move(ptr)) );
assert(result.second);
}
};
works fine with gcc as well as clang as installed in Mac OSX. However, I get some problems with clang 3.5 on linux systems when using -stdlib=libc++ (fine with clang 3.2 and 3.4), where the library apparently is the one installed with clang 3.2 (how to obtain the version of llvm's libc++?), when the compiler gives the error
In file included from test.cc:1: In file included from /cm/shared/apps/llvm/3.2/include/libcxx/string:434: In file included from /cm/shared/apps/llvm/3.2/include/libcxx/algorithm:594: In file included from /cm/shared/apps/llvm/3.2/include/libcxx/memory:599: /cm/shared/apps/llvm/3.2/include/libcxx/tuple:320:11: error: rvalue reference to type 'unique_ptr<[2 * ...]>' cannot bind to lvalue of type 'unique_ptr<[2 * ...]>'
: value(__t.get())
^ ~~~~~~~~~ /cm/shared/apps/llvm/3.2/include/libcxx/tuple:444:8: note: in instantiation of member function 'std::__1::__tuple_leaf<1, std::__1::unique_ptr<int, std::__1::default_delete<int> > &&, false>::__tuple_leaf' requested here struct
__tuple_impl<__tuple_indices<_Indx...>, _Tp...>
^ utils2/test.cc:23:13: note: in instantiation of function template specialization 'std::__1::forward_as_tuple<std::__1::basic_ostringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> > &, std::__1::unique_ptr<int, std::__1::default_delete<int> > >' requested here
std::forward_as_tuple(str,std::move(ptr)));
^ In file included from utils2/test.cc:1: In file included from /cm/shared/apps/llvm/3.2/include/libcxx/string:434: In file included from /cm/shared/apps/llvm/3.2/include/libcxx/algorithm:594: In file included from /cm/shared/apps/llvm/3.2/include/libcxx/memory:599: /cm/shared/apps/llvm/3.2/include/libcxx/tuple:321:10: error: static_assert failed "Can not copy a tuple with rvalue reference member"
{static_assert(!is_rvalue_reference<_Hp>::value, "Can not copy a tuple with rvalue reference member");}
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
What is wrong here, the compiler, the library, or my code? And how can I avoid this problem (with the given compiler/library)?
The following code fails to compile:
#include <cstdio>
#include <sstream>
int main()
{
std::ostrstream strm;
strm.rdbuf()->freeze(0);
}
I get the following errors on compilation:
g++ sample3.cpp
sample3.cpp: In function 'int main()':
sample3.cpp:5: error: 'ostrstream' is not a member of 'std'
sample3.cpp:5: error: expected `;' before 'strm'
sample3.cpp:6: error: 'strm' was not declared in this scope
After searching in google, I suspect that I should use ostringstream in place of ostrstream, so I have modified the program as below:
#include <cstdio>
#include <sstream>
int main()
{
std::ostringstream strm;
strm.rdbuf()->freeze(0);
}
But now I get the following errors:
g++ sample3.cpp
sample3.cpp: In function 'int main()':
sample3.cpp:6: error: 'struct std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >' has no member named 'freeze'
Just scrap the freeze() call -- the current generation std::ostringstream doesn't expose its memory management guts to you like the old ostrstream did. You'll need to rework your original code to let the stringstream manage memory in the way it wants (it'll be much simpler/less error-prone that way!).
I should have changed #include "sstream" to #include "strstream"
then it will not report the error "'ostrstream' is not a member of 'std'".
I'm trying to compile the Statemachine example from boost-mpl (located in libs/mpl/examples/fsm/player2.cpp), but it fails with boost version 1.37 and g++ 4.8.2. With boost version 1.56 and the same compiler, the build succeeds. Unfortunately, due to some platform constraints, I cannot switch to version 1.56.
I'm not expecting anyone to look into the above mentioned lengthy example, therefore I identified a minimal code snippet which illustrates the problem:
#include <boost/mpl/fold.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/placeholders.hpp>
namespace mpl = boost::mpl;
using namespace mpl::placeholders;
//Basic queue datatype
template< class CURRENT, class NEXT >
struct queue_element
{
typedef typename CURRENT::mytype mytype;
};
//type to be put at the end of the queue
struct default_queue_element
{
};
template <class TYPE>
struct wrapper{
typedef TYPE mytype;
};
typedef mpl::vector<wrapper<int>, wrapper<char> > myvector;
//the following fold expression should create this type:
typedef queue_element<wrapper<char>, queue_element<wrapper<int>,
default_queue_element> > this_type_should_be_created;
//This typedef fails to compile with boost Version 1.37,
//but works perfectly with version 1.56
typedef typename
mpl::fold<
myvector
,default_queue_element
,queue_element<_2,_1>
>::type
generate_queue;
With boost 1.37, g++ issues the following errors:
foldtest2.cpp: In instantiation of ‘struct queue_element<mpl_::arg<2>, mpl_::arg<1> >’:
../boost_1_37_0/boost/mpl/aux_/preprocessed/gcc/template_arity.hpp:85:5: required from ‘const int boost::mpl::aux::template_arity_impl<queue_element<mpl_::arg<2>, mpl_::arg<1> >, 1>::value’
../boost_1_37_0/boost/mpl/aux_/preprocessed/gcc/template_arity.hpp:93:5: required from ‘const int boost::mpl::aux::template_arity<queue_element<mpl_::arg<2>, mpl_::arg<1> > >::value’
../boost_1_37_0/boost/mpl/aux_/preprocessed/gcc/template_arity.hpp:98:30: required from ‘struct boost::mpl::aux::template_arity<queue_element<mpl_::arg<2>, mpl_::arg<1> > >’
../boost_1_37_0/boost/mpl/aux_/preprocessed/gcc/apply.hpp:67:8: required from ‘struct boost::mpl::apply2<queue_element<mpl_::arg<2>, mpl_::arg<1> >, default_queue_element, wrapper<int> >’
../boost_1_37_0/boost/mpl/aux_/preprocessed/gcc/fold_impl.hpp:67:85: required from ‘struct boost::mpl::aux::fold_impl<2, boost::mpl::v_iter<boost::mpl::vector<wrapper<int>, wrapper<char> >, 0l>, boost::mpl::v_iter<boost::mpl::vector<wrapper<int>, wrapper<char> >, 2l>, default_queue_element, queue_element<mpl_::arg<2>, mpl_::arg<1> > >’
../boost_1_37_0/boost/mpl/fold.hpp:39:18: required from ‘struct boost::mpl::fold<boost::mpl::vector<wrapper<int>, wrapper<char> >, default_queue_element, queue_element<mpl_::arg<2>, mpl_::arg<1> > >’
foldtest2.cpp:39:6: required from here
foldtest2.cpp:15:38: error: no type named ‘mytype’ in ‘struct mpl_::arg<2>’
typedef typename CURRENT::mytype mytype;
Is there a work-around to make the code compile with boost 1.37? I have been searching the web for quite some time. If nevertheless the question has already been answered somewhere, I would be grateful if you could point that out.
Looks to be very simply a bug in that ancient(¹) version of boost.
A quick bisection tells me it was fixed in v1.43.0(²). Release notes don't disclose the secret, but git does:
c5621d9 MPL: merge fix for ticket #1992 boost::mpl::zip_view does not support use as a metafunction with ::type
31a2c78 MPL: merge fix for ticket #4061 [MPL] gcc-4.5 compilation problems related to arity_helper
It clearly appears to be the latter (confirmed by compiling against 31a2c78).
So your fix this single line in include/boost/mpl/aux_/template_arity.hpp(³):
sizeof(arity_helper(type_wrapper<F>(),arity_tag<N>())) - 1
should be
sizeof(::boost::mpl::aux::arity_helper(type_wrapper<F>(),arity_tag<N>())) - 1
Of course the proper way to fix this is to use a supported version of boost
¹ (November 3rd, 2008)!!
² (May 6th, 2010)
³ warning: also present in several copies generated in preprocessed versions of the header
I was using the topcoder C++ compiler, and although this code just run fine in Linux gcc, the topcoder compiler gave this error:
your code did not compile:
errors compiling:
Your class or method was improperly declared: In function
‘std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >,
std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > _wrapper::thunk(std::string)’:
Your class or method was improperly declared:20034:
error: conversion from ‘void’ to non-scalar type
‘std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >,
std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >’ requested
This is the code snippet where it is flagging error:
class BinaryCode {
public:
static int get_digit(char c)
{
return (c-'0');
}
void decode(string decd)
{
int i;
std::vector <int> decoded(decd.size());
std::transform(decd.begin(), decd.end(), decoded.begin(), &get_digit);
int length=decoded.size();
This is the topcoder problem description:
Definition Class:BinaryCode
Method:decode
Parameters:string
Returns:vector <string>
Method signature:
vector <string> decode(string message)
(be sure your method is public)
Your method signature is:
void decode(string decd)
Should be:
vector <string> decode(string message)
TopCoder compiles your code with testing code for the problem. Make sure the code you provide meets the requirements in the problem statement.
Topcoder compiler is expecting the function to be
vector <string> decode(string message)
while your function is
void decode(string message)
You are using 'void' instead of vector < string >
Try to use
using namespace std;
it fixed my problem. And also includes, it puts your code into separate file
#include <vector>
#include <string>
I'm trying to use the boost unordered_multimap class and I'm having trouble declaring it. The error follows the code.
#include <iostream>
#include <map> // header file needed for to use MAP STL
#include <boost/unordered_map.hpp>
using namespace std;
int main(void)
{
map<int, map<int, map<int,int> > > _3dstl;
_3dstl[0][0][0] = 10;
cout<<_3d[0][0][0]<<endl;
typedef boost::unordered_multimap<int, typedef boost::unordered_multimap<int, int>> unordered_map;
unordered_map _2d;
return 0;
}
This is the error:
||In function 'int main()':|
|17|error: template argument 2 is invalid|
|17|error: template argument 5 is invalid|
|17|warning: 'typedef' was ignored in this declaration|
|18|error: 'unordered_map' was not declared in this scope|
|18|error: expected ';' before 'location3d'|
||=== Build finished: 4 errors, 1 warnings ===|
Change this line:
typedef boost::unordered_multimap<int, typedef boost::unordered_multimap<int, int>> unordered_map;
To this:
typedef boost::unordered_multimap<int, boost::unordered_multimap<int, int> > unordered_map;
The second typedef is not necessary and a syntax error. Also in C++2003 you have to watch out for >> in template declarations.
Also, please use a different name then unordered_map for the typedef, since this will collide with std::unordered_map if you use using namespace std; (which is IMO a bad practice). A suggestion would be intmap2d or something.