[英]Pushing a string to a private member vector
我有一個std::vector<std::string> tileSet_
作為 class 的成員變量。 在所述 class 的構造函數中,我調用 function
void MapWindow::shuffleTiles() const
{
std::vector<std::pair<std::string, unsigned>> tileAmounts = {
{"a", 2}, {"b", 4}, {"c", 1}, {"d", 4},
{"e", 5}, {"f", 2}, {"g", 1}, {"h", 3},
{"i", 2}, {"j", 3}, {"k", 3}, {"l", 3},
{"m", 2}, {"n", 3}, {"o", 2}, {"p", 3},
{"q", 1}, {"r", 3}, {"s", 2}, {"t", 1},
{"u", 8}, {"v", 9}, {"w", 4}, {"x", 1}
};
int tileN = 0;
for (std::vector<std::pair<std::string, int>>::size_type i = 0;
i < tileAmounts.size();i++) {
tileN += tileAmounts.at(i).second;
}
// only used once to initialise (seed) engine
std::random_device rd;
// random-number engine used (Mersenne-Twister in this case)
std::mt19937 rng(rd());
// guaranteed unbiased
std::uniform_int_distribution<int> uni(0,tileAmounts.size() - 1);
for (int i = 0; i < tileN; i++) {
auto random_integer = uni(rng);
//qDebug() << i << ":" << random_integer;
std::cout << i
<< ":"
<< tileAmounts.at(random_integer).first
<< ":"
<< tileAmounts.at(random_integer).second
<< std::endl
<< std::endl;
while (tileAmounts.at(random_integer).second < 1) {
std::cout << "Reshuffling "
<< tileAmounts.at(random_integer).first
<< ":"
<< tileAmounts.at(random_integer).second
<< std::endl;
random_integer = uni(rng);
}
std::string s = tileAmounts[random_integer].first;
std::cout << s
<< std::endl;
tileSet_.push_back(s);
tileAmounts.at(random_integer).second -= 1;
}
for (std::vector<std::string>::size_type i = 0;
i < tileSet_.size();i++) {
std::cout << tileSet_.at(i)
<< std::endl;
}
}
最后,它試圖從包含在向量tileAmounts
中的一對隨機挑選的字符串推回成員向量。 出於某種原因,這會引發錯誤
no matching member function call to 'push_back'
為什么會發生這種情況。 我正在將向量從std::pair
推回,但這肯定不會導致任何類型不匹配? 包含在std::vector
中的std::pair
中的 std:: std::string
仍然是std::string
,不是嗎?
我花了一段時間才把它全部建成(有一些猜測)。
問題是該方法被標記為const
。 因此,您不能在成員上調用任何變異函數(即,您不能在任何成員變量上調用非 const 成員函數或非 const 成員函數)。
void MapWindow::shuffleTiles() const
^^^^^
...
tileSet_.push_back(s); // Attempt to mutate the state of the object.
這在編譯器生成的錯誤消息中有更詳細的解釋:
t2.cpp:61:14: error: no matching member function for call to 'push_back'
tileSet_.push_back(s);
~~~~~~~~~^~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:712:36: note: candidate function not viable: 'this' argument has type
'const std::vector<std::string>' (aka 'const vector<basic_string<char, char_traits<char>, allocator<char> > >'), but method is not marked const
_LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:715:36: note: candidate function not viable: 'this' argument has type
'const std::vector<std::string>' (aka 'const vector<basic_string<char, char_traits<char>, allocator<char> > >'), but method is not marked const
_LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
如果你 go 到行尾,它會說:“但是方法沒有被標記為 const ”
解密線路:
注意:候選 function 不可行:“this”參數具有類型
'const std::vector',但方法未標記為 const
無效 push_back(const_reference __x);注意:候選 function 不可行:“this”參數具有類型
'const std::vector',但方法未標記為 const
無效 push_back(value_type&& __x);
我找出了問題的根源:向量tileSet_
以前不是成員變量,但我這樣做是出於必要。 I had, however, originally defined the function shuffleTiles
as a const
function, which of course makes it impossible for it to change the state of the class. 從定義中刪除const
解決了這個問題。
C++ 中的錯誤消息肯定很神秘。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.