c++ - I'm getting a error when I declare a class as a member of other class. error : a class-key must be used when declaring a friend -


i c++ rookie , experimenting boost serialization , wanted see if works when class declared member of class. when compile code loads of errors. tried declaring baseds struct no change in errors. 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);         1 << mydata;     }      ifstream ifs("steps.txt");     {         boost::serialization::archive two(ifs);         2 >> 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: class-key must used when declaring friend tier2.cpp:29: error: class-key must used when declaring 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 function) tier2.cpp:38: error: (each undeclared identifier reported once each function appears in.) tier2.cpp:39: error: `y' undeclared (first use function) tier2.cpp:40: error: `lag' undeclared (first use 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 function) tier2.cpp:53: error: expected `;' before "ofs" tier2.cpp:55: error: `archive' not member of `boost::serialization' tier2.cpp:55: error: expected `;' before "one" tier2.cpp:56: error: `one' undeclared (first use function) tier2.cpp:57: error: expected `;' before '}' token tier2.cpp:59: error: `ifstream' undeclared (first use function) tier2.cpp:59: error: expected `;' before "ifs" tier2.cpp:61: error: `archive' not member of `boost::serialization' tier2.cpp:61: error: expected `;' before "ones" tier2.cpp:62: error: `ones' undeclared (first use 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 @ end of file 

you re-defining both baseds , superior. should error similar to

class baseds{};   // definition class superior{}; // definition  // second definition class baseds { private:  .... 

error: redefinition of 'class baseds'

remove first pair of definitions.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -