关于set.insert的一个问题,是否是常量的问题(未解决问题)
生活随笔
收集整理的這篇文章主要介紹了
关于set.insert的一个问题,是否是常量的问题(未解决问题)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?里面有5個文件
//folder.h #ifndef FOLDER_H #define FOLDER_H #include <string> #include <iostream> #include "message.h" #include <set> using namespace std; class Message; class Folder{public:Folder() = default;void addMsg(Message * rhs);void remMsg(Message * rhs);void print_msg()const;private:set<Message*> messages; };#endif//message.h #ifndef MESSAGE_H #define MESSAGE_H#include <string> #include <vector> #include <new> #include <set> #include "folder.h"using namespace std;class Folder; class Message{friend class Folder;public:explicit Message(const string &str = "");Message(const Message&);Message& operator=(const Message &);~Message();//從給定的Folder集合中添加/刪除本Messagevoid save(Folder &);void remove(Folder &);bool compare(const Message &m1,const Message &m2){return (m1.contents > m2.contents); }void print_folders()const{for(auto & i : folders)cout << i << endl; }private:string contents; //消息文本set<Folder*> folders; //包含本Message的Folder//拷貝構造函數、拷貝賦值運算符和析構函數所使用的工具函數//將本Message添加到指向參數的Folder中void add_to_Folders(const Message &);//從folders中的每個Folder中刪除本Messagevoid remove_from_Folders(); };#endif//fod_functions.cc #include <string> #include <iostream> #include <memory> #include <new> #include "folder.h" #include "message.h" using namespace std; void Folder::addMsg(Message *rhs) {messages.insert(rhs); } void Folder::remMsg(Message *rhs) {messages.erase(rhs); } void Folder::print_msg()const { for(auto & m : messages)cout << m << endl; }//main.cc #include <string> #include <iostream> #include "message.h" #include "folder.h" using namespace std; int main() {Folder f1,f2,f3,f4;Folder* fd1,*fd2,*fd3,*fd4;fd1 = &f1;fd2 = &f2;fd3 = &f3;fd4 = &f4;Message m1("hello,word-1..."),m2("hello,world-2"),m3("hello,world-3"),m4("hello,word-4");Message *a1,*a2,*a3,*a4;a1 = &m1;a2 = &m2;a3 = &m3;a4 = &m4;m1.save(f1);m1.save(f2);m1.save(f3);m1.save(f4);m1.print_folders();cout << "folder:" << endl;cout << fd1 << endl;cout << fd2 << endl;cout << fd3 << endl;cout << fd4 << endl;m2.save(f2);cout << "message:" << endl;fd1->print_msg();fd2->print_msg();fd3->print_msg();fd4->print_msg();cout << &m1<< endl;cout << &m2 << endl;cout << &m3 << endl;cout << &m4 << endl;return 0; }//msg_functions.cc #include <memory> #include <new> #include <string> #include <iostream> #include "message.h" #include "folder.h" using namespace std; Message::Message(const string &msg):contents(msg){} Message::Message(const Message &rhs):contents(rhs.contents),folders(rhs.folders){add_to_Folders(rhs);} void Message::add_to_Folders(const Message & rhs) { for(auto & f : rhs.folders){f->addMsg(this); }} void Message::remove_from_Folders() { for(auto & f : folders){f->remMsg(this); } } void Message::save(Folder &rhs) {folders.insert(&rhs); //添加的是文件的地址,用&運算符rhs.addMsg(this); // 添加消息的地址,是this }void Message::remove(Folder &rhs) { folders.erase(&rhs); rhs.remMsg(this); } Message & Message::operator=(const Message & rhs) { for(auto f : folders){f -> remMsg(this); //or remove_from_Folders()} contents = rhs.contents; folders = rhs.folders; for(auto f : rhs.folders){f->addMsg(this); //or add_to_Folders(rhs);}return *this; }Message::~Message() {remove_from_Folders(); }在類Folder.h中,為何成員函數addMsg和remMsg的參數不能是const的,如果參數是const的,g++編譯器會如下報錯:
fod_functions.cc: In member function ‘void Folder::addMsg(const Message*)’: fod_functions.cc:10:21: error: no matching function for call to ‘std::set<Message*>::insert(const Message*&)’10 | messages.insert(rhs);| ^ In file included from /usr/include/c++/9/set:61,from message.h:6,from folder.h:5,from fod_functions.cc:5: /usr/include/c++/9/bits/stl_set.h:509:7: note: candidate: ‘std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = Message*; _Compare = std::less<Message*>; _Alloc = std::allocator<Message*>; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree_const_iterator<Message*>; std::set<_Key, _Compare, _Alloc>::value_type = Message*]’ <near match>509 | insert(const value_type& __x)| ^~~~~~ /usr/include/c++/9/bits/stl_set.h:509:7: note: conversion of argument 1 would be ill-formed: fod_functions.cc:10:18: error: invalid conversion from ‘const Message*’ to ‘std::set<Message*>::value_type’ {aka ‘Message*’} [-fpermissive]10 | messages.insert(rhs);| ^~~| || const Message* In file included from /usr/include/c++/9/set:61,from message.h:6,from folder.h:5,from fod_functions.cc:5: /usr/include/c++/9/bits/stl_set.h:518:7: note: candidate: ‘std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(std::set<_Key, _Compare, _Alloc>::value_type&&) [with _Key = Message*; _Compare = std::less<Message*>; _Alloc = std::allocator<Message*>; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree_const_iterator<Message*>; std::set<_Key, _Compare, _Alloc>::value_type = Message*]’ <near match>518 | insert(value_type&& __x)| ^~~~~~ /usr/include/c++/9/bits/stl_set.h:518:7: note: conversion of argument 1 would be ill-formed: fod_functions.cc:10:18: error: invalid conversion from ‘const Message*’ to ‘std::set<Message*>::value_type’ {aka ‘Message*’} [-fpermissive]10 | messages.insert(rhs);| ^~~| || const Message* In file included from /usr/include/c++/9/set:61,from message.h:6,from folder.h:5,from fod_functions.cc:5: /usr/include/c++/9/bits/stl_set.h:546:7: note: candidate: ‘std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::insert(std::set<_Key, _Compare, _Alloc>::const_iterator, const value_type&) [with _Key = Message*; _Compare = std::less<Message*>; _Alloc = std::allocator<Message*>; std::set<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator<Message*>; std::set<_Key, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<Message*>; std::set<_Key, _Compare, _Alloc>::value_type = Message*]’546 | insert(const_iterator __position, const value_type& __x)| ^~~~~~ /usr/include/c++/9/bits/stl_set.h:546:7: note: candidate expects 2 arguments, 1 provided /usr/include/c++/9/bits/stl_set.h:551:7: note: candidate: ‘std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::insert(std::set<_Key, _Compare, _Alloc>::const_iterator, std::set<_Key, _Compare, _Alloc>::value_type&&) [with _Key = Message*; _Compare = std::less<Message*>; _Alloc = std::allocator<Message*>; std::set<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator<Message*>; std::set<_Key, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<Message*>; std::set<_Key, _Compare, _Alloc>::value_type = Message*]’551 | insert(const_iterator __position, value_type&& __x)| ^~~~~~ /usr/include/c++/9/bits/stl_set.h:551:7: note: candidate expects 2 arguments, 1 provided /usr/include/c++/9/bits/stl_set.h:566:2: note: candidate: ‘template<class _InputIterator> void std::set<_Key, _Compare, _Alloc>::insert(_InputIterator, _InputIterator) [with _InputIterator = _InputIterator; _Key = Message*; _Compare = std::less<Message*>; _Alloc = std::allocator<Message*>]’566 | insert(_InputIterator __first, _InputIterator __last)| ^~~~~~ /usr/include/c++/9/bits/stl_set.h:566:2: note: template argument deduction/substitution failed: fod_functions.cc:10:21: note: candidate expects 2 arguments, 1 provided10 | messages.insert(rhs);| ^ In file included from /usr/include/c++/9/set:61,from message.h:6,from folder.h:5,from fod_functions.cc:5: /usr/include/c++/9/bits/stl_set.h:578:7: note: candidate: ‘void std::set<_Key, _Compare, _Alloc>::insert(std::initializer_list<_Tp>) [with _Key = Message*; _Compare = std::less<Message*>; _Alloc = std::allocator<Message*>]’578 | insert(initializer_list<value_type> __l)| ^~~~~~ /usr/include/c++/9/bits/stl_set.h:578:43: note: no known conversion for argument 1 from ‘const Message*’ to ‘std::initializer_list<Message*>’578 | insert(initializer_list<value_type> __l)| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~ fod_functions.cc: In member function ‘void Folder::remMsg(const Message*)’: fod_functions.cc:14:17: error: invalid conversion from ‘const Message*’ to ‘std::set<Message*>::key_type’ {aka ‘Message*’} [-fpermissive]14 | messages.erase(rhs);| ^~~| || const Message* In file included from /usr/include/c++/9/set:61,from message.h:6,from folder.h:5,from fod_functions.cc:5: /usr/include/c++/9/bits/stl_set.h:684:29: note: initializing argument 1 of ‘std::set<_Key, _Compare, _Alloc>::size_type std::set<_Key, _Compare, _Alloc>::erase(const key_type&) [with _Key = Message*; _Compare = std::less<Message*>; _Alloc = std::allocator<Message*>; std::set<_Key, _Compare, _Alloc>::size_type = long unsigned int; std::set<_Key, _Compare, _Alloc>::key_type = Message*]’684 | erase(const key_type& __x)總結
以上是生活随笔為你收集整理的关于set.insert的一个问题,是否是常量的问题(未解决问题)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql没法登录
- 下一篇: pygame的一个小问题,未解决