简体   繁体   English

传递std向量作为参考:没有匹配的函数来调用

[英]Passing std vector as reference: no matching function to call

I can't see what's wrong with my code. 我看不出我的代码有什么问题。 I have a an std::vector which is a private member of my class Foo. 我有一个std :: vector,它是我的类Foo的私有成员。 It is not declared as const, even when the error the compiler gives suggests so. 它不会声明为const,即使编译器给出的错误也是如此。

(At Foo.h) (在Foo.h)

private:
std::vector<std::string> tableBackup;

I'm calling a function (from Foo.cpp): 我正在调用一个函数(来自Foo.cpp):

BackupTable(this->tableBackup);

This method is into DatabaseLoad.cpp and .h: 这个方法是在DatabaseLoad.cpp和.h中:

public:
void BackupTable(std::vector<std::string> &tableBackup);

Defined as: 定义为:

void DatabaseLoad::BackupTable(std::vector<std::string> &tableBackup){
//whatever...
}

I'm getting the following error when I call the method from Foo.cpp: 当我从Foo.cpp调用该方法时,我收到以下错误:

No matching function for call to 'DatabaseLoad::BackupTable(const std::vector<std::basic_string<char> > &)'

What's the problem? 有什么问题? Currently using C++11, but I guess this has nothing to do with that. 目前正在使用C ++ 11,但我想这与此无关。

You are calling the BackupTable function in a context where the DatabaseLoad object is const -qualified, therefore the compiler is expecting a call to a const -reference. 您在DatabaseLoad对象为const限定的上下文中调用BackupTable函数,因此编译器希望调用const引用。

If you are not planning on modifying the vector, you should declare the function as: 如果您不打算修改向量,则应将该函数声明为:

void BackupTable(const std::vector<std::string>& tableBackup);

我猜你是从Foo类的const方法调用BackupTable的

You seem to be calling BackupTable(this->tableBackup); 你似乎在调用BackupTable(this->tableBackup); inside a member function which is qualified as const . 在一个成员函数里面,它被限定为const This means that this is of type const Whatever* , and thus all data members are implicitly const -qualified inside this member function as well. 这意味着, this是类型的const Whatever* ,因此,所有的数据成员被隐式const -qualified该成员函数内为好。 So they cannot be bound to a non- const reference. 因此,它们不能绑定到非const引用。

You have two sane options: 你有两个理智的选择:

  1. If BackupTable does not modify its argument, it should accept it as const & instead of just & . 如果BackupTable不修改其参数,则应将其接受为const &而不是just &

  2. If it does modify its argument, it means the calling function modifies its this object, so it should not be marked as const . 如果它确实修改了它的参数,则意味着调用函数修改了它的this对象,因此它不应该被标记为const

A third (far less likely) option is that tableBackup is actually an implementation details of your class and the fact that it changes does not affect the "logical const ness" of the class. 第三种(更不可能)选项是tableBackup实际上是类的实现细节,并且它更改的事实不会影响类的“逻辑const ”。 If that is so, you can mark it as mutable (that way, even const functions will be able to modify it). 如果是这样,你可以将它标记为mutable (这样,即使const函数也能修改它)。 At the same time, you must introduce some form of synchronisation mechanism (eg a mutex) whenever you access the mutable tableBackup (or any mutable member). 同时,每当访问mutable tableBackup (或任何可变成员)时,都必须引入某种形式的同步机制(例如互斥锁)。 The reason is that all of the standard library expects const operations to be thread-safe. 原因是所有标准库都期望const操作是线程安全的。 An emerging idiom for this is adding a private member like this: 一个新兴的成语是添加这样的私人成员:

mutable std::mutex mutables;

And locking mutables whenever you access (even just for reading!) a mutable member. 每当你访问(甚至只是为了阅读!)一个可变成员时锁定mutables

I think that 'this' is const in ' BackupTable(this->tableBackup);'. 我认为'this'在'BackupTable(this-> tableBackup);'中是常量。 You call the ' BackupTable' in a 'Foo() const' function. 你在'Foo()const'函数中调用'BackupTable'。

没有匹配的 function 调用 'std::vector <std.. '< div><div id="text_translate"><p> 我正在解决的问题是:我必须采用<strong>T</strong>测试用例。 For each test case I have to take string as an input, then I need to arrange the input string as: <em>string at even position {double space} string at odd position</em> (example: <em>input</em> - StackOverflow, <em>output</em> - <strong>Sakvrlw</strong> <strong>tcOefo</strong> ). 我编写了以下代码,其中我为所有测试用例获取输入并将其存储在向量中。 然后我将 vector 的元素分配给另一个声明的 string s。</p><pre> #include &lt;cmath&gt; #include &lt;cstdio&gt; #include &lt;vector&gt; #include &lt;iostream&gt; #include &lt;algorithm&gt; #include &lt;string&gt; using namespace std; int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int T,i; cout &lt;&lt; "Enter no. of test cases: "; cin &gt;&gt; T; vector&lt;string&gt; v; vector&lt;string&gt; odd; vector&lt;string&gt; even; string str; for(int i=0; i&lt;T; i++){ cin &gt;&gt; str; v.push_back(str); } string s; for(i=0; i&lt;v.size(); i++){ s = v.at(i); for(i=0; i&lt;s.size(); i++){ if(i==0){ even.push_back(s[i]); //*This is where I am getting error*. }else if(i==1){ odd.push_back(s[i]); }else{ if(i%2==0){ even.push_back(s[i]); }else{ odd.push_back(s[i]); } } } for(i=0; i&lt;even.size(); i++){ cout &lt;&lt; even.at(i); } cout &lt;&lt; " "; for(i=0; i&lt;odd.size(); i++){ cout &lt;&lt; odd.at(i); } cout &lt;&lt; endl; even.clear(); odd.clear(); s.clear(); } return 0; }</pre><p> 在编译上面的代码时,我得到"no matching error for call std::vector..." 。 <strong><em>我到底做错了什么?</em></strong></p></div></std..> - no matching function for call to 'std::vector<std.. '

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有匹配的 function 调用 'std::vector <std.. '< div><div id="text_translate"><p> 我正在解决的问题是:我必须采用<strong>T</strong>测试用例。 For each test case I have to take string as an input, then I need to arrange the input string as: <em>string at even position {double space} string at odd position</em> (example: <em>input</em> - StackOverflow, <em>output</em> - <strong>Sakvrlw</strong> <strong>tcOefo</strong> ). 我编写了以下代码,其中我为所有测试用例获取输入并将其存储在向量中。 然后我将 vector 的元素分配给另一个声明的 string s。</p><pre> #include &lt;cmath&gt; #include &lt;cstdio&gt; #include &lt;vector&gt; #include &lt;iostream&gt; #include &lt;algorithm&gt; #include &lt;string&gt; using namespace std; int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int T,i; cout &lt;&lt; "Enter no. of test cases: "; cin &gt;&gt; T; vector&lt;string&gt; v; vector&lt;string&gt; odd; vector&lt;string&gt; even; string str; for(int i=0; i&lt;T; i++){ cin &gt;&gt; str; v.push_back(str); } string s; for(i=0; i&lt;v.size(); i++){ s = v.at(i); for(i=0; i&lt;s.size(); i++){ if(i==0){ even.push_back(s[i]); //*This is where I am getting error*. }else if(i==1){ odd.push_back(s[i]); }else{ if(i%2==0){ even.push_back(s[i]); }else{ odd.push_back(s[i]); } } } for(i=0; i&lt;even.size(); i++){ cout &lt;&lt; even.at(i); } cout &lt;&lt; " "; for(i=0; i&lt;odd.size(); i++){ cout &lt;&lt; odd.at(i); } cout &lt;&lt; endl; even.clear(); odd.clear(); s.clear(); } return 0; }</pre><p> 在编译上面的代码时,我得到"no matching error for call std::vector..." 。 <strong><em>我到底做错了什么?</em></strong></p></div></std..> - no matching function for call to 'std::vector<std.. ' 没有匹配的函数来调用&#39;merge(std :: vector <int> &,std :: vector <int> &) - No matching function for call to 'merge(std::vector<int>&, std::vector<int>&) 没有匹配的函数来调用 std::vector<float> - No matching function to call to std::vector<float> 没有用于调用std :: vector的匹配函数 <std::tuple> 推回 - no matching function for call to std::vector<std::tuple> push_back 错误:没有匹配的 function 调用 'recherche(std::vector &gt;&amp;, std::vector &gt;::iterator, std::vector &gt;::iterator, const char [10])' - error: no matching function for call to ‘recherche(std::vector >&, std::vector >::iterator, std::vector >::iterator, const char [10])’ 将std :: vector传递给函数 - Passing std::vector to function 传递std :: vector作为指针引用 - Passing a std::vector as a pointer reference 错误:没有匹配的 function 调用 'std::vector::erase(int)' - error: no matching function for call to ‘std::vector::erase(int)’ 没有匹配的函数调用&#39;std::vector &gt;::push_back(char&amp;)&#39; - no matching function for call to 'std::vector >::push_back(char&)' C ++线程与以std :: vector为参数的函数调用不匹配 - C++ threads not matching function call with std::vector as argument
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM