[英]How to fix “no instance of overloaded function” in vector push_back?
I want to write a function which takes as input a pointer to a vector pointer which point to a string (Dictionary) and a pointer which points to a char (p). 我想编写一个函数,该函数将指向一个向量指针的指针作为输入,该向量指针指向一个字符串(字典),一个指针指向一个char(p)。 The function will check if the char is in the Dictionary and if it isn't there it adds the p in the vector Dictionary.
该函数将检查char是否在Dictionary中,如果不存在,它将在向量Dictionary中添加p。
My code: 我的代码:
#include <iostream>
#include <string>
#include <vector>
using std::string;
using std::vector;
std::vector<string *> dictionary;
void manageDictionary(vector<string *> * dictionary, char *p) {
for (unsigned int i = 0; i < (*dictionary).size(); i++) {
string * pstring = (*dictionary).at(i);
if ((*pstring).compare(p)) {
(*dictionary).push_back(p);
}
}
}
However, the visual studio compiler shows I have an error in the if statement just before the push_back method ( .
). 但是,Visual Studio编译器显示在push_back方法(
.
)之前的if语句中有错误。 When I hover on the error, it says "no instance of overloaded function". 当我将鼠标悬停在错误上时,它说“没有重载函数实例”。
I added the std::vector<string *> dictionary;
我添加了
std::vector<string *> dictionary;
at the beginning, still cannot figure out where the problem is. 一开始,仍然无法弄清楚问题出在哪里。
dictionnary
is a vector of std::string*
. dictionnary
是std::string*
的向量。 std::string*
and char*
are totally unrelated types. std::string*
和char*
是完全不相关的类型。 To convert from char*
to std::string*
will require you to create a new string
that contains the value of p
for your dictionnary, rather than passing a char*
directly. 从
char*
转换为std::string*
要求您创建一个包含字典值p
的新string
,而不是直接传递char*
。 This change will allow your example to compile, but the resulting function is error prone. 此更改将允许您的示例进行编译,但是生成的函数容易出错。
#include <string>
#include <vector>
using std::string;
using std::vector;
void manageDictionnary(vector<string *> * dictionnary, char *p) {
for (unsigned int i = 0; i < (*dictionnary).size(); i++) {
string * pstring = (*dictionnary).at(i);
if ((*pstring).compare(p)) {
(*dictionnary).push_back(new string(p));
// Make a new string ^^^^^^^^^^
}
}
}
This solution will require you to delete your strings manually which is not the way things are done in c++. 此解决方案将要求您手动删除字符串,而这不是用c ++完成的方式。 Changing from
std::vector<std::string*>
to simply std::vector<std::string>
will solve this problem, and avoid you headaches in the future. 从
std::vector<std::string*>
更改为简单的std::vector<std::string>
将解决此问题,并避免将来出现麻烦。 There are other unnecessary pointers that can be removed. 还有其他可以删除的不必要的指针。 Since
at(i)
returns a string&
then we should change pstring
to string&
. 由于
at(i)
返回string&
因此我们应该将pstring
更改为string&
。 Since dictionnary
is not optional (can't be nullptr
) and always points to the same vector
we can also change it to a vector<string>&
. 由于
dictionnary
不是可选的(不能为nullptr
),并且始终指向同一vector
我们也可以将其更改为vector<string>&
。
void manageDictionnary(vector<string> & dictionnary, char *p) {
for (unsigned int i = 0; i < dictionnary.size(); i++) {
string & pstring = dictionnary.at(i);
if (pstring.compare(p)) {
dictionnary.push_back(p);
}
}
}
This latest version will work fine and is much more in line with c++'s philosophy for resource management. 这个最新版本可以正常工作,并且更符合c ++的资源管理理念。 I recommend you read on a few topics :
我建议您阅读一些主题:
std::find
. std::find
。 Additionally, consider using std::set<string>
or std::unordered_set<string>
for a more convenient representation of a dictionnary. 另外,可以考虑使用
std::set<string>
或std::unordered_set<string>
来更方便地表示字典。
In the future, note that the preferred way to access a pointer's methods is ptr->foo()
rather than (*ptr).foo()
. 将来,请注意,访问指针方法的首选方法是
ptr->foo()
而不是(*ptr).foo()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.