【发布时间】:2019-08-10 07:51:41
【问题描述】:
我正在尝试创建一个模拟“便签”活动的基本应用程序。这将包含添加注释和删除注释的功能。下面是代码。在 deleteNote 函数中,我正在使用 std::find 方法作为输入参数给出的 Notes 向量中的标题。 std::find API 引发编译错误。下面是代码。
#include <iostream>
#include <vector>
#include <utility>
#include <tuple>
#include <algorithm>
using InitializerTags = std::initializer_list<std::string>;
using TupleObject = std::tuple<std::string, std::string, std::string>;
class Note
{
public:
TupleObject m_tags;
Note(std::string title, std::string text, std::string tags){
std::cout<< "parameterized Constructor"<< std::endl;
m_tags = std::make_tuple(title, text, tags);
}
/*Note(const Note& rhs){
std:: cout << "copy constructor"<< std::endl;
m_tags = rhs.m_tags;
}*/
Note(Note&& rhs){
std::cout<< "move constructor"<< std::endl;
m_tags = rhs.m_tags;
}
Note& operator=(Note&& rhs){
std::cout << "move assignment"<< std::endl;
if(this != &rhs){
m_tags = rhs.m_tags;
}
return *this;
}
Note() = delete;
Note(const Note& rhs) = delete;
Note& operator=(const Note& rhs) = delete;
~Note(){
}
};
class Storyboard
{
private:
std::vector <Note> m_notes;
public:
/*Storyboard(){
m_notes.reserve(1);
}*/
void addNote(std::string title, std::string text, std::string tags)
{
std::cout << "inside addNote"<< std::endl;
m_notes.emplace_back(title, text, tags);
}
void deleteNote(std::string title)
{
for(auto& x: m_notes){
if(std::get<0>(x.m_tags) == title){
m_notes.erase(std::find(m_notes.begin(),m_notes.end(), x));
}
}
}
void print()
{
std::cout << "Inside print"<< std::endl;
for(auto& x : m_notes){
std::cout << std::get<0>(x.m_tags)<< " ";
std::cout << std::get<1>(x.m_tags)<< " ";
std::cout << std::get<2>(x.m_tags)<< " ";
std::cout << std::endl;
}
}
};
以下是错误。
在 /usr/include/c++/5/bits/stl_algobase.h:71:0 包含的文件中,
来自 /usr/include/c++/5/bits/char_traits.h:39,
从 /usr/include/c++/5/ios:40,
来自 /usr/include/c++/5/ostream:38,
来自 /usr/include/c++/5/iostream:39,
来自 StoryBoard.cpp:1:
/usr/include/c++/5/bits/predefined_ops.h:在 'bool __gnu_cxx::__ops::_Iter_equals_val<_value>::operator()(_Iterator) 的实例化中 [with _Iterator = __gnu_cxx::__normal_iterator
我检查了发生错误的文件。
std::find 的签名出现了问题,它是 std::find(_IIter, _IIter, const _Tp&)
第 3 个输入参数被视为 const 引用,并将其与预定义_ops.h:194 中的非 const 引用进行比较。
我试图了解我的代码中导致这种情况的原因。
还试图找出解决办法。
任何有助于我理解的帮助将不胜感激。
【问题讨论】:
-
这部分错误信息似乎是最相关的:“error: no match for 'operator==' (operand types are 'Note' and 'const Note')”简而言之,你无法比较两个
Note对象是否相等