[英]STL Push_back string in vector
I am trying to push a string in a string vector, like below 我试图在字符串向量中推送一个字符串,如下所示
void Node::set_val(string &val)
{
this->val.push_back(val);
}
But when I try to call it as below 但是,当我尝试将其称为如下
Obj.set_val("10h;");
I get the below error, 我得到以下错误,
error: no matching function for call to 'Node::set_val(const char [5])'
I assumed that the string in " " is same as string
in c++, Why do I get such an error? 我假设“”中的string
与c ++中的string
相同,为什么会出现这样的错误? What has to be changed below? 下面有什么要改变的?
You are taking in a std::string
by non-const reference. 您通过非const引用接收std::string
。 Non-const references cannot bind to rvalues, like "10h;"
非const引用不能绑定到rvalues,例如"10h;"
, so you can't pass literals in to that function. ,所以你不能将文字传递给那个函数。
If you aren't going to modify the argument, you should take your argument by reference-to-const: 如果你不打算修改参数,你应该通过reference-to-const来获取你的参数:
void Node::set_val(const string &val)
// ^^^^^
This way, a temporary std::string
will be constructed from your const char[5]
and passed in to set_val
. 这样,临时的std::string
将从你的const char[5]
构造并传递给set_val
。
You could improve this by taking in the string
by value and move
ing it into the vector
: 您可以通过按值获取string
并将其move
到vector
来改善这一点:
void Node::set_val(string val)
{
this->val.push_back(std::move(val));
}
This prevents you from making some unnecessary copies. 这可以防止您制作一些不必要的副本。
So in C++, const char* is implicitly convertible to std::string because std::string has a (non-explicit) constructor that takes const char*. 所以在C ++中,const char *可以隐式转换为std :: string,因为std :: string有一个带有const char *的(非显式)构造函数。 So what the compiler tries here is to create a temporary std::string object for your function call, like so: 所以编译器在这里尝试的是为函数调用创建一个临时的std :: string对象,如下所示:
Node.set_val(std::string("10h;"));
However, since you declared the parameter of set_val to be a non-const reference to a std::string, the compiler can't make this conversion work due to the fact that temporary objects can't be bound to non-const references. 但是,由于您将set_val的参数声明为对std :: string的非const引用,因此编译器无法使此转换起作用,因为临时对象无法绑定到非const引用。
There are three ways to make this work, depending on what you want to achieve: 根据您想要实现的目标,有三种方法可以实现这一目标:
void Node::set_val(const std::string& val) {}
void Node::set_val(std::string val) {}
void Node::set_val(std::string&& val) {}
All will compile (the last one requires C++11 or higher), but seeing your use case, I would recommend to use the second or third one. 所有都将编译(最后一个需要C ++ 11或更高版本),但看到你的用例,我建议使用第二个或第三个。 For an explanation why, try reading a little bit about move semantics in C++11. 为了解释原因,请尝试阅读一下C ++ 11中的移动语义。
The important thing to take away here is that const char* implicitly converts to std::string by creating a temporary object, and temporary objects can't be passed to functions taking non-const references. 这里要带走的重要一点是,const char *通过创建临时对象隐式转换为std :: string,并且临时对象不能传递给采用非const引用的函数。
You are passing "10h;"
你正在路过"10h;"
which is a const char array. 这是一个const char数组。
Fix it by passing a string: Obj.set_val(string("10h"));
通过传递一个字符串来修复它: Obj.set_val(string("10h"));
and edit function to take a string by value: 并编辑函数以按值获取字符串:
void Node::set_val(string val) { /* */ }
Or maybe better, edit your function to take a const string&
: 或者更好,编辑你的函数来获取一个const string&
:
void Node::set_val(const string &val) { /* */ }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.