我是一名C ++新手,我正在尝试使用boost序列化,我想看看当一个类被声明为另一个类的成员时它是否有效。但是当我编译我的代码时,我会遇到很多错误。我尝试将基于声明为结构,但没有更改错误。我的代码:
#include <iostream>
#include <fstream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
class baseds{};
class superior{};
class baseds {
private:
friend class boost::serialization::access;
public:
int a;
int b;
int c;
baseds(){}
~baseds(){}
template <class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & a;
ar & b;
ar & c;
}
};
class superior {
private:
friend class boost::serialization::access;
public:
int x;
int y;
baseds lag;
superior(){}
~superior(){}
template <class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & x;
ar & y;
ar & lag;
}
};
int main() {
superior myData,myData2;
myData.x=10;
myData.y=20;
myData.lag.a=1;
myData.lag.b=2;
myData.lag.c=3;
ofstream ofs("steps.txt");
{
boost::serialization::archive one(ofs);
one << myData;
}
ifstream ifs("steps.txt");
{
boost::serialization::archive two(ifs);
two >> myData2;
}
std::cout<<"\n"<<myData2.x;
std::cout<<"\n"<<myData2.y;
std::cout<<"\n"<<myData2.lag.a;
std::cout<<"\n"<<myData2.lag.b;
std::cout<<"\n"<<myData2.lag.c;
return 0;
}
错误:
tier2.cpp:10: error: a class-key must be used when declaring a friend
tier2.cpp:29: error: a class-key must be used when declaring a friend
tier2.cpp:32: error: expected `;' before "int"
tier2.cpp: In member function `void superior::serialize(Archive&, unsigned int)':
tier2.cpp:38: error: `x' undeclared (first use this function)
tier2.cpp:38: error: (Each undeclared identifier is reported only once for each function it appears in.)
tier2.cpp:39: error: `y' undeclared (first use this function)
tier2.cpp:40: error: `lag' undeclared (first use this function)
tier2.cpp: In function `int main()':
tier2.cpp:47: error: 'class superior' has no member named 'x'
tier2.cpp:48: error: 'class superior' has no member named 'y'
tier2.cpp:49: error: 'class superior' has no member named 'lag'
tier2.cpp:50: error: 'class superior' has no member named 'lag'
tier2.cpp:51: error: 'class superior' has no member named 'lag'
tier2.cpp:53: error: `ofstream' undeclared (first use this function)
tier2.cpp:53: error: expected `;' before "ofs"
tier2.cpp:55: error: `archive' is not a member of `boost::serialization'
tier2.cpp:55: error: expected `;' before "one"
tier2.cpp:56: error: `one' undeclared (first use this function)
tier2.cpp:57: error: expected `;' before '}' token
tier2.cpp:59: error: `ifstream' undeclared (first use this function)
tier2.cpp:59: error: expected `;' before "ifs"
tier2.cpp:61: error: `archive' is not a member of `boost::serialization'
tier2.cpp:61: error: expected `;' before "ones"
tier2.cpp:62: error: `ones' undeclared (first use this function)
tier2.cpp:63: error: expected `;' before '}' token
tier2.cpp:64: error: 'class superior' has no member named 'x'
tier2.cpp:65: error: 'class superior' has no member named 'y'
tier2.cpp:66: error: 'class superior' has no member named 'lag'
tier2.cpp:67: error: 'class superior' has no member named 'lag'
tier2.cpp:68: error: 'class superior' has no member named 'lag'
tier2.cpp:71:2: warning: no newline at end of file
答案 0 :(得分:1)
您正在重新定义baseds
和superior
。您应该收到类似于
class baseds{}; // definition
class superior{}; // definition
// second definition
class baseds {
private:
....
错误:重新定义'class baseds'
删除第一对定义。
答案 1 :(得分:1)
您要定义baseds
和superior
两次。
在gcc上你会得到正确的错误信息:
main.cpp:9:7: error: redefinition of 'baseds'
class baseds {
^
main.cpp:6:7: note: previous definition is here
class baseds{};
^
main.cpp:27:7: error: redefinition of 'superior'
class superior {
^
main.cpp:7:7: note: previous definition is here
class superior{};
看起来您的编译器无法识别重新定义并丢失,从而产生错误的错误消息。
如果您打算提供两个类的前向声明,则需要丢失大括号:
class baseds;
class superior;
class baseds {
/* ... */
};
但是你根本不使用superior
来定义baseds
,你可以完全忽略前向声明 - 对于superior
的定义你需要 baseds
的定义,因为您拥有该类型的成员,因此前向声明是不够的。
答案 2 :(得分:0)
对不起堆叠流量的公民,这是一个愚蠢的问题,在睡了一觉之后想出了大部分的错误 正确的代码如下:
#include <iostream>
#include <fstream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
class baseds;
class superior;
class baseds {
private:
friend class boost::serialization::access;
public:
int a;
int b;
int c;
baseds(){}
~baseds(){}
template <class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & a;
ar & b;
ar & c;
}
};
class superior {
private:
friend class boost::serialization::access;
friend class baseds;
public:
int x;
int y;
baseds lag;
superior(){}
~superior(){}
template <class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & x;
ar & y;
ar & lag;
}
};
int main() {
superior myData,myData2;
myData.x=10;
myData.y=20;
myData.lag.a=1;
myData.lag.b=2;
myData.lag.c=3;
std::ofstream ofs("steps.txt");
{
boost::archive::text_oarchive one(ofs);
one << myData;
}
std::ifstream ifs("steps.txt");
{
boost::archive::text_iarchive two(ifs);
two >> myData2;
}
std::cout<<"\n"<<myData2.x;
std::cout<<"\n"<<myData2.y;
std::cout<<"\n"<<myData2.lag.a;
std::cout<<"\n"<<myData2.lag.b;
std::cout<<"\n"<<myData2.lag.c;
return 0;
}