问题
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
回答1:
You are re-defining both baseds
and superior
. You should get an error similar to
class baseds{}; // definition
class superior{}; // definition
// second definition
class baseds {
private:
....
error: redefinition of 'class baseds'
Remove the first pair of definitions.
回答2:
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.
回答3:
Sorry fellow stackoverflow citizens, this was a stupid question, figured out most of the errors after a good sleep The correct code is as below:
#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;
friend class baseds;
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;
std::ofstream ofs("steps.txt");
{
boost::archive::text_oarchive one(ofs);
one << myData;
}
std::ifstream ifs("steps.txt");
{
boost::archive::text_iarchive 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;
}
来源:https://stackoverflow.com/questions/21575324/im-getting-a-error-when-i-declare-a-class-as-a-member-of-other-class-error-a