【发布时间】:2020-11-17 23:54:08
【问题描述】:
我有一个课程,在代码中我粘贴了我认为问题所在的部分。
class SubtreeExplorer : public AbstractTask {
public:
SubtreeExplorer(Threadpool& tp, SudokuBoard&& sudoku)
: tp(tp), sudoku(std::move(sudoku)) {}
...
private:
Threadpool& tp;
SudokuBoard sudoku;
bool sudoku_backtracking_search(SudokuBoard& s) {
...
while(...){
...
// let another thread explore the subtree
tp.submit(make_shared<SubtreeExplorer>(tp, SudokuBoard(sudoku)));
}
}
};
当我尝试编译它时,它给出了错误:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:4325:5: error: static_assert failed due to requirement 'is_constructible<SubtreeExplorer,
Threadpool &, SudokuBoard &>::value' "Can't construct object in make_shared"
static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in make_shared" );
我正在使用 VS Code,在 IDE 中没有突出显示错误。
问题可能是由make_shared 引起的,但我无法弄清楚导致它的SubtreeExplorer 类的构造函数中是什么。
Sudokuboard(sudoku) 从给定的板返回一个新板,几乎没有修改。我希望将这个新板移动到新的子树资源管理器中。
在谷歌上看,我写的构造函数应该把板子移进去,但我仍然得到我写的编译错误,加上一长串难以阅读的笔记:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2259:9: note: in instantiation of function template specialization
'std::__1::__compressed_pair_elem<SubtreeExplorer, 1, false>::__compressed_pair_elem<Threadpool &, SudokuBoard &, 0, 1>' requested here
_Base2(__pc, _VSTD::move(__second_args),
^ /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:3672:16: note: in instantiation of function template specialization
'std::__1::__compressed_pair<std::__1::allocator<SubtreeExplorer>, SubtreeExplorer>::__compressed_pair<std::__1::allocator<SubtreeExplorer> &,
Threadpool &, SudokuBoard &>' requested here
: __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
^ /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:4331:26: note: in instantiation of function template specialization
'std::__1::__shared_ptr_emplace<SubtreeExplorer, std::__1::allocator<SubtreeExplorer>
>::__shared_ptr_emplace<Threadpool &, SudokuBoard &>' requested
here
::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
^ /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:4710:29: note: in instantiation of function template specialization
'std::__1::shared_ptr<SubtreeExplorer>::make_shared<Threadpool &, SudokuBoard &>' requested here
return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
^ src/sudoku_parallel.cpp:119:13: note: in instantiation of function template specialization 'std::__1::make_shared<SubtreeExplorer, Threadpool &, SudokuBoard
&>' requested here tp.submit(make_shared<SubtreeExplorer>(tp, sudoku));
^ src/sudoku_parallel.cpp:41:3: note: candidate constructor not viable: no known conversion from 'SudokuBoard' to 'SudokuBoard &&' for 2nd argument SubtreeExplorer(Threadpool& tp, SudokuBoard&& sudoku) ^ src/sudoku_parallel.cpp:39:7: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 2 were provided class SubtreeExplorer : public AbstractTask {
^ src/sudoku_parallel.cpp:39:7: note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 2 were provided
这让我觉得我向构造函数传递了错误数量的参数,即使它需要两个参数并且我传递了两个参数,一个线程池和一个数独板。
基本上,SubtreeExplorer 的创建类似于 sudoku_backtracking_search 函数的主体:
int main() {
...
SubokuBoard sudoku(filepath);
tp.submit(make_shared<SubtreeExplorer>(tp, sudoku));
...
}
编辑: tp 是一个线程池,它的提交方法有这个签名:
bool submit(std::shared_ptr<AbstractTask> task);
AbstractTask 类如下:
struct AbstractTask {
virtual ~AbstractTask() = default;
virtual void run() = 0;
};
【问题讨论】:
-
使用构造函数的代码在哪里?请发minimal reproducible example
-
@cigien 不是
tp.submit(make_shared<SubtreeExplorer>(tp, SudokuBoard(sudoku))); -
哦,好的,但是
submit等是什么?如果我们可以简单地复制您显示的代码并重现错误,那就太好了。 -
@cigien 我知道,但这是一个分成多个文件的代码。所以我必须粘贴所有的头文件和实现文件。我不认为这是可行的。我将编辑并插入提交的签名。
-
@idclev463035818 这是标准库实现的代码。
标签: c++ constructor c++17 shared-ptr move-semantics