c++ "error: invalid use of void expression"

c++ "error: invalid use of void expression"

我正在尝试实现 trie,但出现以下错误:

error: invalid use of void expression
  5   |   collect(node->next[c], pre.push_back(c), q);
      |   ^~~~~~~

这是我的功能:

void string_map<T>::collect(Node* node, string pre, std::queue<string> q) const {
    if (node == nullptr) {return;}
    if (node->definition != nullptr) {q.push(pre); }
    for (int c = 0; c < 256; c++) { 
        collect(node->next[c], pre.push_back(c), q); 
    }
}

这是我遇到的唯一错误。其他一切正常。

有什么帮助吗?请。

http://www.cplusplus.com/reference/string/string/push_back/

string::push_back returns 是 Void 类型(即,不是 return)并且您正在使用此调用的 return 值作为参数这里:

collect(node->next[c], pre.push_back(c), q); 

我不确定你到底想做什么,但这就是错误的原因。