【问题标题】:Why am I getting no known conversion error?为什么我没有收到任何已知的转换错误?
【发布时间】:2017-09-22 15:33:18
【问题描述】:

我有这样的代码: Link to Wandbox

当我尝试编译它时,我得到了这个:

./type_holder.h:45:14: error: no matching constructor for initialization of 'test_class'
                return new T(args...);
                           ^ ~~~~
./type_holder.h:54:35: note: in instantiation of member function 'di::DiConstructor<true, test_class, di::DiMark &&, di::AnyResolver>::construct' requested
      here
                                                         decltype(AnyResolver())>::construct(std::move(DiMark{}), AnyResolver{});
                                                                                   ^
test.cc:26:19: note: in instantiation of function template specialization 'di::ConstructIfPossible<test_class>' requested here
        std::cout << di::ConstructIfPossible<test_class>() << std::endl;
                         ^
test.cc:14:9: note: candidate constructor not viable: no known conversion from 'di::DiMark' to 'di::DiMark &&' for 1st argument
        INJECT(test_class, int* a) {}
               ^
./inject_markers.h:6:36: note: expanded from macro 'INJECT'
#define INJECT(name, ...) explicit name(di::DiMark&& m, __VA_ARGS__)
                                   ^
test.cc:12:7: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 2 were provided
class test_class {
      ^
test.cc:12:7: note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 2 were provided
1 error generated.

问题是我做错了什么?为什么编译器告诉我我正在尝试将DiMark 转换为DiMark&amp;&amp;?既然我做了std::move(DiMark) {} 明确地将左值转换为右值,它不应该是DiMark&amp;&amp; 吗?

【问题讨论】:

  • 如果你能发一个minimal reproducible example会很有帮助。
  • 显示的代码不符合minimal reproducible example的要求,否则无法完全分析。
  • test_class 是什么?
  • 更新了评论并添加了指向 Wandbox 的链接
  • 请在问题中发布问题代码,而不是在场外链接上,这样当您的场外链接中断时,问题仍然具有一些可能的价值。

标签: c++ c++11 metaprogramming std stdmove


【解决方案1】:

错误告诉您的是,您正在尝试使用两个参数构造函数构造 test_class 对象,但您尚未定义任何对象。

有两个隐式定义的构造函数,它们接受一个参数(复制构造函数和移动构造函数)。但是,由于您尝试使用 DiMarkAnyResolver 参数构造 test_class 对象,并且此构造函数不存在,因此您会收到错误。

要解决这个问题,您需要创建一个双参数构造函数。 (然后您可能需要定义复制和移动构造函数,因为如果您定义任何其他构造函数,则不会定义隐式构造函数。)

【讨论】:

  • 我已经更新了我的问题并发布了可复制代码 sn-p 的链接。
猜你喜欢
  • 2021-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-23
  • 2019-12-05
  • 1970-01-01
相关资源
最近更新 更多