簡體   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