问题描述
我是一个C ++菜鸟,我正在试验boost序列化,我想看看它是否工作,当一个类被声明为另一个类的成员。但是当我编译我的代码,我得到了大量的错误。我试图声明基于结构,但没有改变的错误。我的代码:
#include< iostream>
#include< fstream>
#include< boost / archive / text_iarchive.hpp>
#include< boost / archive / text_oarchive.hpp>
class baseds {};
class superior {};
基于类{
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&一个;
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&落后;
}
};
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(stages.txt);
{
boost :: serialization :: archive one(ofs);
one<< myData;
}
ifstream ifs(steps.txt);
{
boost :: serialization :: archive two(ifs);
two>> myData2;
}
std :: cout<<\\\
<< myData2.x;
std :: cout<<\\\
<< myData2.y;
std :: cout<<\\\
<< myData2.lag.a;
std :: cout<<\\\
<< myData2.lag.b;
std :: cout<<\\\
<< myData2.lag.c;
return 0;
}
错误:
tier2.cpp:10:错误:在声明朋友时必须使用类键
tier2.cpp:29:error:class-当声明一个朋友
tier2.cpp:32:error:expected`;'beforeint
tier2.cpp:在成员函数`void superior :: serialize(Archive& ':
tier2.cpp:38:error:`x'undeclared(首先使用此函数)
tier2.cpp:38:error:(每个未声明的标识符仅对每个函数报告一次, 。)
tier2.cpp:39:error:`y'undeclared(首先使用此函数)
tier2.cpp:40:error:`lag'undeclared(首先使用此函数)
tier2.cpp:在函数`int main()':
tier2.cpp:47:error:'class superior'没有成员名为'x'
tier2.cpp:48:error:'class上级没有名为'y'的成员
tier2.cpp:49:error:'class superior'没有名为'lag'的成员
tier2.cpp:50:error:'class superior'has no成员名为'lag'
tier2.cpp:51:error:'class superior'没有成员名为'lag'
tier2.cpp:53:error:`ofstream'undeclared(首先使用此函数)
tier2.cpp:53:error:expected`;'beforeofs
tier2.cpp:55:error:`archive'不是`boost :: serialization'的成员
tier2.cpp:55:error:expected`;'beforeone
tier2.cpp:56:error:`one'undeclared(首先使用此函数)
tier2.cpp:57:error:
tier2.cpp:59:error:`ifstream'undeclared(首先使用此函数)
tier2.cpp:59:error:expected`;'beforeifs
tier2.cpp:61:error:`archive'不是`boost :: serialization'的成员
tier2.cpp:61:error:expected`;'beforeones
tier2.cpp:62:error:`ones'undeclared(首先使用这个函数)
tier2.cpp:63:error:expected`;'before'} token
tier2.cpp:64:错误:'class superior'没有成员命名为'x'
tier2.cpp:65:error:'class superior'没有成员命名为'y'
tier2.cpp:66:error:'class上级'没有名为'lag'的成员
tier2.cpp:67:error:'class superior'没有名为'lag'的成员
tier2.cpp:68:error:'class superior'没有成员名为'lag'
tier2.cpp:71:2:warning:文件末尾没有换行符
您正在定义 baseds
和 superior
p>
在gcc上会得到正确的错误信息:
main.cpp :9:7:错误:重定义'baseds'
类为基础{
^
main.cpp:6:7:注意:以前的定义在这里
class baseds { ;
^
main.cpp:27:7:error:redefinition of'superior'
class superior {
^
main.cpp:7:7:以前的定义是这里
class superior {};
看起来你的编译器不能识别重定义并丢失,产生错误的错误信息。
如果您打算提供两个类的前瞻性声明,您需要丢失大括号:
class baseds;
class superior;
基于类{
/ * ... * /
};
但是你不使用 superior
对于 baseds
的定义,您可以完全忽略前向声明 - 对于 superior
的定义,您需要 baseds
的定义,因为您有该类型的成员,因此向前声明是不够的。
I am a C++ rookie and I was experimenting with boost serialization and I wanted to see if it works when a class is declared as a member of another class. But when I compile my code I get loads of errors. I tried declaring baseds as a struct but no change in errors. My code :
#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;
}
errors:
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
You are defining baseds
and superior
twice.
On gcc you would get the proper error message:
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{};
Seemingly your compiler does not recognize the redefinition and gets lost, producing the poor error message.
If you meant to provide a forward declaration of both classes, you need to lose the braces:
class baseds;
class superior;
class baseds {
/* ... */
};
But sice you don't use superior
for the definition of baseds
at all, you can leave out the forward declarations completely - for the definition of superior
you need the definition of baseds
, since you have a member of that type and therefore a forward declaration is not sufficient.
这篇关于我收到一个错误,当我声明一个类作为其他类的成员。错误:在声明朋友时必须使用类键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!