我试图将我的包传递给另一个函数,它看起来与我在教程中看到的相似,但显然不够相似:
class algorithm
{
typedef uint64_t time_type;
protected:
std::string name;
time_type result;
template <typename... Args>
void execute(const Args&...);
public:
algorithm(std::string name);
//virtual void prepareTest()=0;
template <typename TimeT, typename... Args>
void beginTest(const Args&...);
void printResult();
time_type getResult();
};
template <typename TimeT, typename... Args>
void algorithm::beginTest(const Args&... args)
{
auto start = chrono::high_resolution_clock::now();
execute(args...);
auto duration = chrono::duration_cast<TimeT>
(chrono::high_resolution_clock::now() - start);
result = duration.count();
}
template <typename... Args>
void execute(const Args&... args)
{
//nothing here yet
}
实例化算法和beginTest()时,出现链接器错误:
const int n = 100000;
const int m = 1000;
algortest::algorithm brutus("brutus");
brutus.beginTest<chrono::milliseconds>(n, m);
架构x86_64的未定义符号:“ void algortest :: algorithm :: execute(int const&,int const&)”,引用自:void algortest :: algorithm :: beginTest>,int,int>(int const&,int const&)in main .o ld:找不到架构x86_64的符号
我做错了什么?另外,是否可以在派生的算法类中覆盖execute()?
您忘了execute
with的定义的前缀algorithm::
是全部:-)
它应显示为:
template <typename... Args>
void algorithm::execute(const Args&... args)
{
// ...
}
您可能还希望对参数使用完全转发,而不是强制通过引用传递它们-而不是const Args&...
,在调用函数时使用Args&&...
,然后再std::forward<Args>(args)...
调用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句