我收到以下错误消息:
main.cpp:没有可行的构造函数复制'communal'类型的变量
在第二个构造函数communal( const T& instance )
上,给出以下消息:
data.h:候选构造函数不可行:没有已知的从'communal'到'const int&'的转换第一个参数
转换似乎是倒退。我希望转换来自const int&共同的。有没有办法让隐式转换在这里工作?谢谢你的帮助。
main.cpp中:
communal<int> test_communal1 = 123; // Implicit initialization triggers error
data.cpp:
template<typename T>
struct communal : general_type::communal<T>
{
using super = general_type::communal<T>;
communal() : super( nullptr ) {}
communal( const T& instance ) : super( new T( instance ) ) {}
communal( const T* instance ) : super( new T( instance ) ) {}
communal( communal<T>& instance ) : super( instance ) {}
communal( communal<T>* instance ) : super( instance ) {}
~communal()
{
this->counter->deallocate( [this]()
{
delete this->counter;
delete this->instance;
});
}
};
答案 0 :(得分:0)
我找到了解决方法。我的目的是让以下转换构造函数工作communal( const T& instance )
。我犯了一个错误,就是为我的拷贝构造函数留下const。出于某种原因,推断使用错误的复制构造函数以及从communal<int>
到const int&
的潜在转换。在向构造函数添加const时,它修复了问题。虽然,它似乎确实是一个错误,它不会优先使用转换构造函数。也许更多地研究这个问题,我可以更深入地理解为什么它优先考虑转换构造函数的其他构造函数。虽然,这将不得不等待现在。