繁体   English   中英

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

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

我看不出我的代码有什么问题。 我有一个std :: vector,它是我的类Foo的私有成员。 它不会声明为const,即使编译器给出的错误也是如此。

(在Foo.h)

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

我正在调用一个函数(来自Foo.cpp):

BackupTable(this->tableBackup);

这个方法是在DatabaseLoad.cpp和.h中:

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

定义为:

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

当我从Foo.cpp调用该方法时,我收到以下错误:

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

有什么问题? 目前正在使用C ++ 11,但我想这与此无关。

您在DatabaseLoad对象为const限定的上下文中调用BackupTable函数,因此编译器希望调用const引用。

如果您不打算修改向量,则应将该函数声明为:

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

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

你似乎在调用BackupTable(this->tableBackup); 在一个成员函数里面,它被限定为const 这意味着, this是类型的const Whatever* ,因此,所有的数据成员被隐式const -qualified该成员函数内为好。 因此,它们不能绑定到非const引用。

你有两个理智的选择:

  1. 如果BackupTable不修改其参数,则应将其接受为const &而不是just &

  2. 如果它确实修改了它的参数,则意味着调用函数修改了它的this对象,因此它不应该被标记为const

第三种(更不可能)选项是tableBackup实际上是类的实现细节,并且它更改的事实不会影响类的“逻辑const ”。 如果是这样,你可以将它标记为mutable (这样,即使const函数也能修改它)。 同时,每当访问mutable tableBackup (或任何可变成员)时,都必须引入某种形式的同步机制(例如互斥锁)。 原因是所有标准库都期望const操作是线程安全的。 一个新兴的成语是添加这样的私人成员:

mutable std::mutex mutables;

每当你访问(甚至只是为了阅读!)一个可变成员时锁定mutables

我认为'this'在'BackupTable(this-> tableBackup);'中是常量。 你在'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..> 没有匹配的函数来调用&#39;merge(std :: vector <int> &,std :: vector <int> &) 没有匹配的函数来调用 std::vector<float> 没有用于调用std :: vector的匹配函数 <std::tuple> 推回 错误:没有匹配的 function 调用 'recherche(std::vector &gt;&amp;, std::vector &gt;::iterator, std::vector &gt;::iterator, const char [10])' 将std :: vector传递给函数 传递std :: vector作为指针引用 错误:没有匹配的 function 调用 'std::vector::erase(int)' 没有匹配的函数调用&#39;std::vector &gt;::push_back(char&amp;)&#39; C ++线程与以std :: vector为参数的函数调用不匹配
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM