i你哟 发表于 2023-12-19 11:25:58

C++中关于两个类的交叉使用问题,望人工

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;

FishC 发表于 2023-12-19 11:26:09

这个错误是由于类之间的交叉引用导致的。在C++中,如果两个类互相引用时,其中一个类必须先声明,然后定义其成员函数。

在你的代码中,Folder类和Message类互相引用,所以我们需要在Message类定义之前提供对Folder类的前置声明。将以下代码添加到Message类定义之前:

class Folder;

同样,在Folder类定义之前,需要提供对Message类的前置声明。将以下代码添加到Folder类定义之前:

class Message;

这样就解决了类之间的交叉引用问题。希望能帮到你!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页: [1]
查看完整版本: C++中关于两个类的交叉使用问题,望人工