问题在底部以粗体显示,问题也通过最终的蒸馏代码片段进行了总结.
我试图统一我的类型系统(类型系统从类型到字符串)和单个组件(由Lakos定义).我正在使用boost::array,, boost::variant和boost::mpl,以实现这一目标.我想让我的类型的解析器和生成器规则统一在一个变体中.有一个未定义的类型,一个int4(见下文)类型和一个int8类型.该变体读作variant<undefined, int4,int8>.
int4特征:
struct rbl_int4_parser_rule_definition
{
typedef boost::spirit::qi::rule<std::string::iterator, rbl_int4()> rule_type;
boost::spirit::qi::int_parser<rbl_int4> parser_int32_t;
rule_type rule;
rbl_int4_parser_rule_definition()
{
rule.name("rbl int4 rule");
rule = parser_int32_t;
}
};
template<>
struct rbl_type_parser_rule<rbl_int4>
{
typedef rbl_int4_parser_rule_definition string_parser;
};
Run Code Online (Sandbox Code Playgroud)
上面的变体从未定义开始,然后我初始化规则.我有一个问题,导致50页的错误,我终于设法跟踪它,Variant operator=在分配期间使用,而a boost::spirit::qi::int_parser<>不能分配给另一个(operator =).
相比之下,我的未定义类型没有问题:
struct rbl_undefined_parser_rule_definition
{
typedef boost::spirit::qi::rule<std::string::iterator, void()> rule_type;
rule_type rule;
rbl_undefined_parser_rule_definition()
{
rule.name("undefined parse rule");
rule = boost::spirit::qi::eps;
}
};
template<>
struct rbl_type_parser_rule<rbl_undefined>
{
typedef rbl_undefined_parser_rule_definition string_parser;
};
Run Code Online (Sandbox Code Playgroud)
蒸馏问题:
#include <string>
#include <boost/spirit/include/qi.hpp> …Run Code Online (Sandbox Code Playgroud) 似乎我不能让这个工作.我创建了一个简单的控制台应用程序(依赖于websocket++库),它需要Boost库..但是当我尝试编译时,我得到:
致命错误LNK1104:无法打开文件'libboost_system-vc110-mt-gd-1_51.lib'
但是,我做了创建lib的bjam,(boost_root)/stage/libs并将libs的路径链接到编译器C++/Additionnals includes.
如果我查看(boost_root)/stage/libs文件 libboost_system-vc110-mt-gd-1_51.lib不存在.它被称为libboost_system-vc110-mt-sgd-1_51.lib.
任何的想法?
C++ 0x添加hash<...>(...).
我找不到一个hash_combine函数,如boost中所示.实现这样的事最简洁的方法是什么?也许,使用C++ 0x xor_combine?
我boost:shared_ptr在我的代码中广泛使用.实际上,堆上分配的大多数对象都是由a保存的shared_ptr.不幸的是,这意味着我无法传递this任何需要的函数shared_ptr.考虑以下代码:
void bar(boost::shared_ptr<Foo> pFoo)
{
...
}
void Foo::someFunction()
{
bar(this);
}
Run Code Online (Sandbox Code Playgroud)
这里有两个问题.首先,这不会编译,因为T*构造函数shared_ptr是显式的.其次,如果我强制它构建,bar(boost::shared_ptr<Foo>(this))我将创建第二个指向我的对象的共享指针,最终将导致双删除.
这让我想到了一个问题:是否有任何标准模式可以从其中一个对象的方法中获取您知道的现有共享指针的副本?使用侵入式引用计数是我唯一的选择吗?
我正在尝试使用CMake配置项目,但即使它们位于指定的文件夹中,也无法找到Boost库.我已经指定了Boost_INCLUDE_DIR,Boost_LIBRARYDIR和BOOST_ROOT,但是我仍然收到错误,说CMake无法找到Boost.这种错误的原因是什么?
以下代码导致cl.exe崩溃(MS VS2005).
我试图使用boost bind来创建一个调用myclass方法的函数:
#include "stdafx.h"
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <functional>
class myclass {
public:
void fun1() { printf("fun1()\n"); }
void fun2(int i) { printf("fun2(%d)\n", i); }
void testit() {
boost::function<void ()> f1( boost::bind( &myclass::fun1, this ) );
boost::function<void (int)> f2( boost::bind( &myclass::fun2, this ) ); //fails
f1();
f2(111);
}
};
int main(int argc, char* argv[]) {
myclass mc;
mc.testit();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我有三个文件.main.cpp的内容是
#include<iostream>
#include<QString>
#include "util.h"
int main()
{
using Util::convert2QString;
using namespace std;
int n =22;
QString tmp = convert2QString<int>(n);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
util.h
namespace Util
{
template<class T>
QString convert2QString(T type , int digits=0);
}
Run Code Online (Sandbox Code Playgroud)
util.cpp
namespace Util
{
template<class T>
QString convert2QString(T type, int digits=0)
{
using std::string;
string temp = (boost::format("%1%") % type).str();
return QString::fromStdString(temp);
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用以下命令编译这些文件时,我得到未定义的引用错误
vickey@tb:~/work/trash/template$ g++ main.cpp util.cpp -lQtGui -lQtCore -I. -I/usr/local/Trolltech/Qt-4.8.0/include/QtCore -I/usr/local/Trolltech/Qt-4.8.0/include/QtGui -I/usr/local/Trolltech/Qt-4.8.0/include
/tmp/cca9oU6Q.o: In function `main':
main.cpp:(.text+0x22): undefined reference to `QString …Run Code Online (Sandbox Code Playgroud) 我正在使用Boost程序选项库来解析命令行参数.
我有以下要求:
我怎么处理这个?这是我的代码处理这个,我发现它非常多余,我认为必须有一个容易做的,对吧?
#include <boost/program_options.hpp>
#include <iostream>
#include <sstream>
namespace po = boost::program_options;
bool process_command_line(int argc, char** argv,
std::string& host,
std::string& port,
std::string& configDir)
{
int iport;
try
{
po::options_description desc("Program Usage", 1024, 512);
desc.add_options()
("help", "produce help message")
("host,h", po::value<std::string>(&host), "set the host server")
("port,p", po::value<int>(&iport), "set the server port")
("config,c", po::value<std::string>(&configDir), "set the config path")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help"))
{
std::cout << desc << "\n";
return false;
}
// There …Run Code Online (Sandbox Code Playgroud) 什么是一个相当于static_cast用boost::shared_ptr?
换句话说,我该如何重写以下内容
Base* b = new Derived();
Derived* d = static_cast<Derived*>(b);
Run Code Online (Sandbox Code Playgroud)
什么时候用shared_ptr?
boost::shared_ptr<Base> b(new Derived());
boost::shared_ptr<Derived> d = ???
Run Code Online (Sandbox Code Playgroud) 有没有什么好方法(也是一种简单的方法)使用Boost来读写XML文件?
我似乎无法使用Boost找到任何简单的示例来读取XML文件.你能指出一个使用Boost读取和编写XML文件的简单示例吗?
如果不是Boost,是否有任何好的和简单的库来读写您可以推荐的XML文件?(它必须是C++库)
boost ×10
c++ ×9
boost-bind ×1
boost-spirit ×1
c++11 ×1
cmake ×1
hash ×1
optional ×1
qt ×1
required ×1
shared-ptr ×1
static-cast ×1
std ×1
xml ×1