当我将一个类声明为其他类的成员时,我收到一个错误。错误:声明朋友时必须使用类键

时间:2014-02-06 04:07:49

标签: c++ class boost-serialization

我正在试验boost序列化,我想看看当一个类被声明为另一个类的成员时它是否有效。但是当我编译我的代码时,我会遇到很多错误。我尝试将基础声明为结构但没有chnage。我的代码:

#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 个答案:

没有答案