我有一个具有5个成员的结构,我用std::forward
初始化了所有参数:
#include <iostream>
#include <string>
#include <utility>
struct OutputFilesName {
template<typename T>
OutputFilesName(
T&& result_subdir_name,
T&& result_TSIM_minus,
T&& result_TSIM_plus,
T&& result_minus_total_dat,
T&& result_plus_total_dat,
T&& result_hdl
) :
subdir_name( std::forward<T>(result_subdir_name) ),
TSIM_minus( std::forward<T>(result_TSIM_minus) ),
TSIM_plus( std::forward<T>(result_TSIM_plus) ),
minus_total_dat( std::forward<T>(result_minus_total_dat) ),
plus_total_dat( std::forward<T>(result_plus_total_dat) ),
hdl( std::forward<T>(result_hdl) ) {
}
const std::string subdir_name;
const std::string TSIM_minus;
const std::string TSIM_plus;
const std::string minus_total_dat;
const std::string plus_total_dat;
const std::string hdl;
OutputFilesName() = delete;
};
我在主程序中调用该结构:
int main(){
OutputFilesName ofn(
"a",
"bb",
"ccc",
"dddd",
"eeeee",
"ffffff"
);
}
编译器抛出错误,看来参数不能正确解析:
./test_genHoughMap.cc:37:19: error: no matching constructor for initialization of 'OutputFilesName'
OutputFilesName ofn(
^
./test_genHoughMap.cc:10:3: note: candidate template ignored: deduced conflicting types for parameter 'T'
('char const (&)[2]' vs. 'char const (&)[3]')
OutputFilesName(
^
./test_genHoughMap.cc:8:8: note: candidate constructor (the implicit copy constructor) not viable: requires
1 argument, but 6 were provided
struct OutputFilesName {
^
./test_genHoughMap.cc:8:8: note: candidate constructor (the implicit move constructor) not viable: requires
1 argument, but 6 were provided
./test_genHoughMap.cc:33:3: note: candidate constructor not viable: requires 0 arguments, but 6 were
provided
OutputFilesName() = delete;
^
1 error generated.
我该如何解决?
问题是您的构造函数对所有参数都使用通用类型T
。现在,为第一个参数char[2]
推导第二个char[3]
发生了什么冲突。如果您通过所有相同长度的st it compiles。
请参阅Marek R