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

标签 c++ class boost-serialization

我是一名 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/

相关文章:

c++ - Eigen 中两个矩阵之间的成对差异

c# - ScriptManager.GetCurrent 方法背后的原因

Python:彼此为 "talk"的类

c++ - `boost::serialization::load_construct_data`的实现引发内存访问冲突错误

c++ - 'cout'将整数显示为十六进制

c++ - VS 代码 "command": "make" differs from command line `make` in terminal window

c++ - 从单独的线程访问单独的文件,这是否有效?

c++ - 在 C++ 中重载插入运算符

c++ - boost 从结构列表派生的结构的序列化

c++ - boost::序列化段错误