小编fok*_*oke的帖子

boost :: property_tree XML漂亮打印

我正在使用boost :: property_tree在我的应用程序中读取和写入XML配置文件.但是当我写文件时,输出看起来很丑陋,文件中有很多空行.问题是它应该由人类编辑,所以我想获得更好的输出.

作为一个例子,我写了一个小测试程序:

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>

int main( void )
{
    using boost::property_tree::ptree;
    ptree pt;

    // reading file.xml
    read_xml("file.xml", pt);

    // writing the unchanged ptree in file2.xml
    boost::property_tree::xml_writer_settings<char> settings('\t', 1);
    write_xml("file2.xml", pt, std::locale(), settings);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

file.xml包含:

<?xml version="1.0" ?>
<config>
    <net>
        <listenPort>10420</listenPort>
    </net>
</config>
Run Code Online (Sandbox Code Playgroud)

运行程序后,file2.xml包含:

<?xml version="1.0" encoding="utf-8"?>
<config>



    <net>



        <listenPort>10420</listenPort>
    </net>
</config>
Run Code Online (Sandbox Code Playgroud)

有没有办法获得更好的输出,除了手动通过输出和删除空行?

c++ boost boost-propertytree

31
推荐指数
1
解决办法
2万
查看次数

堆栈跟踪和命名空间中的函数

我试图在执行我的(c ++)程序的某个时刻获得回溯.

因为我正在使用backtrace和backtrace_symbols.这方面的东西:

std::string stacktrace( unsigned int frames_to_skip )
{
    std::string str;

    void* stack_addrs[50];
    int trace_size = backtrace( stack_addrs, 50 );
    char** stack_strings = backtrace_symbols( stack_addrs, trace_size );

    str += "[bt] backtrace:\n";
    // skip frames_to_skip stack frames
    for( int i = frames_to_skip; i < trace_size; ++i )
    {
        char tmp[4096];
        sprintf( tmp, "[bt] #%d %s\n", i-frames_to_skip, stack_strings[i] );
        str += tmp;
    }

    free( stack_strings );

    return str;
}
Run Code Online (Sandbox Code Playgroud)

它有效,但缺少一些函数名称.例:

[bt] #0 /path/to/executable() [0x43e1b5]
[bt] #1 /path/to/executable() [0x43e0cd]
[bt] #2 /path/to/executable() …
Run Code Online (Sandbox Code Playgroud)

c++ gcc g++ backtrace

10
推荐指数
1
解决办法
1203
查看次数

eclipse CDT:重建依赖项目

我正在开发一个 C++ 项目,在 linux 上运行 eclipse Indigo + CDT

它由一个库项目和一个包含该库单元测试的项目组成。
显然,第二个项目取决于第一个项目。
两个项目都使用外部构建器

但是,当我修改第一个项目的标头时,它不会重建测试项目。我无法让它做到这一点...它只会重新链接第二个项目,即使标头包含在测试项目中。这非常烦人,因为我经常在处理库时进行工作构建,然后在某个时候我进行完全重建,然后在我 30 分钟前所做的事情上看到大量错误!

CDT 全局构建控制台的示例输出:

**** Build of configuration Debug for project libxxx ****

make -j all 
Building file: ../foo.cpp
Invoking: GCC C++ Compiler
ccache g++ -I"/home/foke/workspaces/cpp/libxxx/include" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"foo.d" -MT"foo.d" -o "foo.o" "../foo.cpp"
Finished building: ../foo.cpp

Building target: libxxx.a
Invoking: GCC Archiver
ar -r  "libxxx.a"  ./foo.o  ./src/yyy.o  ./src/detail/zzz.o   
ar: creating libxxx.a
Finished building target: libxxx.a


**** Build Finished ****

**** Build of …
Run Code Online (Sandbox Code Playgroud)

c++ eclipse build-process build eclipse-cdt

5
推荐指数
1
解决办法
1817
查看次数

在ViewPager中调用片段上的方法

我想做的就是调用Fragment类的函数.但我似乎无法找到一种方法来访问ViewPager中我的片段的实例.

我的活动的xml:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" />
Run Code Online (Sandbox Code Playgroud)

没有这样我不能调用findFragmentById()(可以吗?)我也不知道给findFragmentByTag()提供什么,它总是返回null:/

android android-fragments android-fragmentactivity

3
推荐指数
1
解决办法
9792
查看次数

强制g ++零初始化内存

我想写一些我正在编写的库的单元测试

我有一个看起来像这样的课程:

class A
{
public:
    A(B* pB)
    // construction details, involves my library
    // but ultimately there is a new being done ,)

    B* m_pB;
};
Run Code Online (Sandbox Code Playgroud)

我想检查指针m_pB是否实际初始化,所以我沿着这些方向做了一些事情:

A* p = // creation of this object involves my library
BOOST_REQUIRE( p->m_pB != NULL );
Run Code Online (Sandbox Code Playgroud)

但它发生了g ++没有对内存进行零初始化,因此值p->m_pB只是随机的.当我new对象时,有没有办法强制g ++为我初始化这个内存?

我相信Visual Studio会根据特定代码执行类似的操作,具体取决于内存的分配位置.

编辑:我现在可以想到2个备份解决方案:使用智能指针,或编写新的运算符......

c++ g++

1
推荐指数
2
解决办法
880
查看次数