c++ - 将字符串推送到私有(private)成员 vector

标签 c++ vector compiler-errors c++14 push-back

我有一个 std::vector<std::string> tileSet_作为类的成员变量。在所述类的构造函数中,我调用函数

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;
  }
}

最后尝试从 vector tileAmounts 中包含的一对中推回随机选择的字符串进入成员 vector 。出于某种原因,这会引发错误

no matching member function call to 'push_back'

为什么会这样。我正在将 vector 从 std::pair 推回,但肯定不会导致任何类型不匹配? std::stringstd::pair包含在 std::vector 中仍然是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);

如果您转到行尾,它会显示:“但方法未标记为常量

解密行:

note: candidate function not viable: 'this' argument has type
'const std::vector', but method is not marked const
void push_back(const_reference __x);

note: candidate function not viable: 'this' argument has type
'const std::vector', but method is not marked const
void push_back(value_type&& __x);

关于c++ - 将字符串推送到私有(private)成员 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58628485/

相关文章:

c++ - 判断 std::vector 是否已重新放置其数组的最佳方法是什么?

c++ - 向 vector 添加多个值

c++ - 如何遍历 vector 并知道元素的索引?

c# - 使用变量否定 'is' 运算符仅适用于 '!' 但不适用于 '== false'

C++ 缩小错误

c++ - 通过循环设计用于搜索文件的另一个函数从文件中打印出信息 - C++

c++ - 为什么 Visual C++ 无法编译继承自私有(private)嵌套类的友元模板?

python - Python-初学者问题

C++/QT : "run" a QAction

c++ - 获取文件创建或修改时间?