gpt4 book ai didi

c++ - 当我将一个类声明为其他类的成员时出现错误。错误 : a class-key must be used when declaring a friend

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

我是一名 C++ 菜鸟,我正在试验 boost 序列化,我想看看当一个类被声明为另一个类的成员时它是否有效。但是当我编译我的代码时,我得到了很多错误。我尝试将 bases 声明为结构,但错误没有变化。我的代码:

#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

最佳答案

您正在重新定义 basessuperior。你应该得到一个类似的错误

class baseds{};   // definition
class superior{}; // definition

// second definition
class baseds {
private:
....

error: redefinition of 'class baseds'

删除第一对定义。

关于c++ - 当我将一个类声明为其他类的成员时出现错误。错误 : a class-key must be used when declaring a friend,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21575324/

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