gpt4 book ai didi

c++ - 分配给指向 std::list 类型的共享指针不起作用

转载 作者:行者123 更新时间:2023-11-30 04:05:03 24 4
gpt4 key购买 nike

我正在尝试将值分配给指向 std::list 的共享指针,但分配失败并给出错误,下面是我的代码供引用:

struct X
{
public:
X();
X(int a,int b, std::string ss)
{
y=a;
z=b;
aa=ss;
}

int y;
int z;
std::string aa;
typedef std::list<X> Xdata;

private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &at, const unsigned int version)
{
at & y;
at & z;
at & aa;
}
};

struct Q
{
public:
Q();
std::string q1;
boost::shared_ptr<X::Xdata> xx;

private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &at, const unsigned int version)
{
at & q1;
at & xx; // will this serialize ??
}
};


void function_assignvalue(Q *e)
{
e->q1 = "hello";

X datax;
datax.y=10;
datax.z=20;
datax.aa="x";

e->xx.reset(new X::Xdata()); // gives error
}

它给出了一些未解析的符号的错误并且还会 xx 序列化?

最佳答案

为您的 XQ 类提供默认构造函数(参见下面的代码):

struct X
{
public:
X() : a(0), b(0) {}
X(int a,int b, std::string ss) : y(a), z(b), aa(ss) {}

int y;
int z;
std::string aa;
typedef std::list<X> Xdata;

private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &at, const unsigned int version)
{
at & y;
at & z;
at & aa;
}
};

struct Q
{
public:
Q() {}
std::string q1;
boost::shared_ptr<X::Xdata> xx;

private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &at, const unsigned int version)
{
at & q1;
at & xx; // will this serialize ??
}
};

关于c++ - 分配给指向 std::list 类型的共享指针不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23467766/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com