|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
class Folder;
class Message;
int main()
{
return 0;
}
class Message
{
friend class Folder;
private:
string contents;//保存消息文本
set<Folder*> folders;//保存指向本消息的所有Folder的指针
void add_to_Folders(const Message& m);//将其添加到指向该参数的Folder中
void remove_from_Folders();//将其从每个含有该文本的folder中删除改内容
void swap(Message &lhs, Message &rhs);
public:
explicit Message(const string str = ""):contents(str){}
Message(const Message& m);
Message& operator=(const Message& rhs);
void save(Folder& f);
void remove(Folder& f);
~Message();
};
void Message::save(Folder& f){
folders.insert(&f);//将给定的Folder添加到列表中
f.addMsg(this);
}
void Message::remove(Folder& f){
folders.erase(&f);
f.remMsg(this);
}
void Message::add_to_Folders(const Message& m){
for(auto f : m.folders){
f->addMsg(this);
}
}
void Message::remove_from_Folders(){
for(auto f : folders){
f->remMsg(this);
}
}
Message::Message(const Message& m):contents(m.contents),folders(m.folders)
{
add_to_Folders(m);//拷贝构造,将拷贝的Message指针添加到m中的folder去
}
Message& Message::operator=(const Message& rhs){
remove_from_Folders();
contents = rhs.contents;
folders = rhs.folders;
add_to_Folders(rhs);
return *this;
}
void Message::swap(Message &lhs, Message &rhs)
{
using std::swap;
//将每个消息的指针从它原来所在的Floders中删除
for (auto f : lhs.folders)
f->remMsg(&lhs);
for (auto f : rhs.folders)
f->remMsg(&rhs);
swap(lhs.folders, rhs.folders);
swap(lhs.contents, rhs.contents);
// 将每个消息的指针添加到新的Floders中
for (auto f : lhs.folders)
f->addMsg(&lhs);
for (auto f : rhs.folders)
f->addMsg(&rhs);
}
Message::~Message()
{
remove_from_Folders();
}
class Folder
{
friend class Message;
private:
set<Message*> me;
void add_to_Messages(const Folder& m);
void remove_from_Messages();
public:
Folder() = default;
Folder(const Folder& fo);
~Folder();
void addMsg(Message* m);
void remMsg(Message* m);
Folder& operator=(const Folder& f) {
remove_from_Messages();
me = f.me;
add_to_Messages(f);
return *this;
}
};
void Folder::addMsg(Message* m){
me.insert(m);
}
void Folder::remMsg(Message* m){
me.erase(m);
}
void Folder::add_to_Messages(const Folder& m){//在message中增加地址
for(auto i : m.me){
i->folders.insert(this);
}
}
void Folder::remove_from_Messages(){//在message中删除地址
for(auto i : me){
i->folders.erase(this);
}
}
Folder::Folder(const Folder& fo):me(fo.me)
{
add_to_Messages(fo);
}
Folder::~Folder()
{
remove_from_Messages();
}
D:\Visual Studio Code\code\C++\2023.9.16\1.cpp: In member function 'void Message::save(Folder&)':
D:\Visual Studio Code\code\C++\2023.9.16\1.cpp:48:2: error: invalid use of incomplete type 'class Folder'
f.addMsg(this);
^
In file included from D:\Visual Studio Code\code\C++\2023.9.16\1.cpp:10:
D:\Visual Studio Code\code\C++\2023.9.16\1.h:52:7: note: forward declaration of 'class Folder'
class Folder;
^~~~~~
D:\Visual Studio Code\code\C++\2023.9.16\1.cpp: In member function 'void Message::remove(Folder&)':
D:\Visual Studio Code\code\C++\2023.9.16\1.cpp:52:2: error: invalid use of incomplete type 'class Folder'
f.remMsg(this);
^
In file included from D:\Visual Studio Code\code\C++\2023.9.16\1.cpp:10:
D:\Visual Studio Code\code\C++\2023.9.16\1.h:52:7: note: forward declaration of 'class Folder'
class Folder;
^~~~~~
D:\Visual Studio Code\code\C++\2023.9.16\1.cpp: In member function 'void Message::add_to_Folders(const Message&)':
D:\Visual Studio Code\code\C++\2023.9.16\1.cpp:56:4: error: invalid use of incomplete type 'class Folder'
f->addMsg(this);
^~
In file included from D:\Visual Studio Code\code\C++\2023.9.16\1.cpp:10:
D:\Visual Studio Code\code\C++\2023.9.16\1.h:52:7: note: forward declaration of 'class Folder'
class Folder;
^~~~~~
D:\Visual Studio Code\code\C++\2023.9.16\1.cpp: In member function 'void Message::remove_from_Folders()':
D:\Visual Studio Code\code\C++\2023.9.16\1.cpp:61:4: error: invalid use of incomplete type 'class Folder'
f->remMsg(this);
^~
In file included from D:\Visual Studio Code\code\C++\2023.9.16\1.cpp:10:
D:\Visual Studio Code\code\C++\2023.9.16\1.h:52:7: note: forward declaration of 'class Folder'
class Folder;
^~~~~~
D:\Visual Studio Code\code\C++\2023.9.16\1.cpp: In member function 'void Message::swap(Message&, Message&)':
D:\Visual Studio Code\code\C++\2023.9.16\1.cpp:80:4: error: invalid use of incomplete type 'class Folder'
f->remMsg(&lhs);
^~
In file included from D:\Visual Studio Code\code\C++\2023.9.16\1.cpp:10:
D:\Visual Studio Code\code\C++\2023.9.16\1.h:52:7: note: forward declaration of 'class Folder'
class Folder;
^~~~~~
D:\Visual Studio Code\code\C++\2023.9.16\1.cpp:82:4: error: invalid use of incomplete type 'class Folder'
f->remMsg(&rhs);
^~
In file included from D:\Visual Studio Code\code\C++\2023.9.16\1.cpp:10:
D:\Visual Studio Code\code\C++\2023.9.16\1.h:52:7: note: forward declaration of 'class Folder'
class Folder;
^~~~~~
D:\Visual Studio Code\code\C++\2023.9.16\1.cpp:87:4: error: invalid use of incomplete type 'class Folder'
f->addMsg(&lhs);
^~
In file included from D:\Visual Studio Code\code\C++\2023.9.16\1.cpp:10:
D:\Visual Studio Code\code\C++\2023.9.16\1.h:52:7: note: forward declaration of 'class Folder'
class Folder;
^~~~~~
D:\Visual Studio Code\code\C++\2023.9.16\1.cpp:89:4: error: invalid use of incomplete type 'class Folder'
f->addMsg(&rhs);
^~
In file included from D:\Visual Studio Code\code\C++\2023.9.16\1.cpp:10:
D:\Visual Studio Code\code\C++\2023.9.16\1.h:52:7: note: forward declaration of 'class Folder'
class Folder; |
|