2

我有一个包含 7 种类型的 boost 变体。当我尝试使用最后两种类型时,链接器会出现段错误。我在 64 位 linux 机器上使用 g++(SuSE Linux 上的 gcc 版本 3.3.3),我得到的错误是

collect2: ld terminated with signal 11 [Segmentation fault]

我将类型放入的顺序无关紧要,最后两个在我尝试使用它们时会导致段错误。任何想法为什么会发生这种情况?

代码:

typedef boost::tuple<std::string, Class1::Ptr> Class1Tuple;
typedef boost::tuple<std::string, Class2::Ptr> Class2Tuple;
typedef boost::tuple<std::string, Class3::Ptr> Class3Tuple;
typedef boost::tuple<std::string, Class4::Ptr> Class4Tuple;
typedef boost::tuple<std::string, Class5::Ptr> Class5Tuple;
typedef boost::tuple<std::string, Class6::Ptr> Class6Tuple;
typedef boost::tuple<std::string, Class7::Ptr> Class7Tuple;

typedef boost::variant< Class1Tuple, Class2Tuple, Class3Tuple,
                        Class4Tuple, Class5Tuple, Class6Tuple,
                        Class7Tuple > ClassTupleItem;

ClassX::Ptr 是指向该类的 boost 共享指针。Ptr 在类本身内部定义为 typedef,如下所示

struct Class1
{
 typedef boost::shared_ptr<Class1> Ptr;
    ...
    ...
}

当我尝试使用 boost 变体中的最后两种类型时

Class1Tuple tup("str", pointer);
ClassTupleItem(tup); // works fine since I used Class1Tuple

Class6Tuple tup2("str", pointer2);
ClassTupleItem(tup2); // causes a segfault.

如果我将 boost::variant 定义为(交换 Class6 和 Class1)

typedef boost::variant< Class6Tuple, Class2Tuple, Class3Tuple,
                        Class4Tuple, Class5Tuple, Class1Tuple,
                        Class7Tuple > ClassTupleItem;

然后在编译此代码时出现段错误

Class1Tuple tup("str", pointer);
ClassTupleItem(tup); // worked earlier
4

1 回答 1

1

它看起来像一个编译器/链接器错误:任何 C++ 代码都不应该在编译器/链接器中导致段错误。

顺便问一下,你如何让这段代码编译?如何pointer声明?

Class1Tuple tup("str", pointer);
ClassTupleItem(tup); // works fine since I used Class1Tuple

Class6Tuple tup2("str", pointer);
ClassTupleItem(tup2); // causes a segfault.

如果类是这样声明的, for Class1Tuplepointer应该是 a shared_ptr<Class1>,并且 forClass6Tuple它应该是不同的类型,shared_ptr<Class6>

struct Class1
{
    typedef boost::shared_ptr<Class1> Ptr;
    /* ... */
};

/* ... */

struct Class6
{
    typedef boost::shared_ptr<Class6> Ptr;
    /* ... */
};

编辑:以下代码使用 g++ 3.3.6 正确编译。我目前无法在 gcc 3.3.3 和 SUSE Linux 上对其进行测试。请尝试编译它,看看链接器是否仍然给出段错误。

#include <boost/shared_ptr.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/variant.hpp>
#include <string>

struct Class1
{
    typedef boost::shared_ptr<Class1> Ptr;
    /* .... */
};

struct Class2
{
    typedef boost::shared_ptr<Class2> Ptr;
    /* .... */
};

struct Class3
{
    typedef boost::shared_ptr<Class3> Ptr;
    /* .... */
};

struct Class4
{
    typedef boost::shared_ptr<Class4> Ptr;
    /* .... */
};

struct Class5
{
    typedef boost::shared_ptr<Class5> Ptr;
    /* .... */
};

struct Class6
{
    typedef boost::shared_ptr<Class6> Ptr;
    /* .... */
};

struct Class7
{
    typedef boost::shared_ptr<Class7> Ptr;
    /* .... */
};

typedef boost::tuple<std::string, Class1::Ptr> Class1Tuple;
typedef boost::tuple<std::string, Class2::Ptr> Class2Tuple;
typedef boost::tuple<std::string, Class3::Ptr> Class3Tuple;
typedef boost::tuple<std::string, Class4::Ptr> Class4Tuple;
typedef boost::tuple<std::string, Class5::Ptr> Class5Tuple;
typedef boost::tuple<std::string, Class6::Ptr> Class6Tuple;
typedef boost::tuple<std::string, Class7::Ptr> Class7Tuple;

typedef boost::variant< Class1Tuple, Class2Tuple, Class3Tuple,
                        Class4Tuple, Class5Tuple, Class6Tuple,
                        Class7Tuple > ClassTupleItem;

int main()
{
    Class1::Ptr pointer;
    Class1Tuple tup("str", pointer);
    (ClassTupleItem(tup)); // Temporary object
    ClassTupleItem item(tup);

    Class6::Ptr pointer2;
    Class6Tuple tup2("str", pointer2);
    (ClassTupleItem(tup2)); // Temporary object
    ClassTupleItem item2(tup2);
}
于 2010-02-24T11:11:16.653 回答