错误:调用'function namel'没有匹配函数

error: no matching function for call to 'function namel'

本文关键字:函数 function 调用 错误 namel      更新时间:2023-10-16

我的最终目标是计算二次理想的幂,该二次理想使用 GMP 库在 C 中作为结构变量实现。

我得到了一个库(ANTL(,其中包含使用C++模板,命名空间和NTL的通用优化幂。此库对 NTL 类型ZZ_p等和基本类型(如长整型、浮点数等(执行幂运算。

我必须使用 ANTL 库来实现我的最终目标 - 计算理想C 结构变量的功效。由于我从未使用过模板和命名空间,因此我想实现基本mpz_t变量的强大功能,以了解一切工作原理。

现在,我有四个头文件 exp.hpp、expbin.hpp、impl.hpp、com.hpp 和一个主文件 exp.cpp如下所示 -

.COM。人乳酸

#ifndef GMPL_COM_H
#define GMPL_COM_H
#include <gmp.h>
namespace GMPL {
template < class T >
inline void assign(T C, const T A)
{
mpz_set(C, A);
}
template<class T>
void mul (T C, const T A, const T B)
{
mpz_mul(C, A, B);
}
template<class T>
void sqr (T C, const T A)
{
mpz_mul(C, A, A);
}
}
#endif // guard

经验。人乳酸

#ifndef EXP_H
#define EXP_H
#include "com.hpp"
namespace GMPL
{
template < class T >
class exp
{
public:
exp() {};
virtual ~exp() {};
virtual void power (T C, const T A, const NTL::ZZ & n) = 0;
};
} // GMPL
#endif // EXP_H

呵呵。人乳酸

#ifndef EXPBIN_H
#define EXPBIN_H
#include "exp.hpp"
namespace GMPL
{
template < class T >
class expbin : public exp<T>
{
public:
expbin() {};
~expbin() {};
void power (T C, const T A, const NTL::ZZ & n);
};
} // GMPL
// Unspecialized template definitions.
#include "impl.hpp"
#endif // EXPBIN_H

英普尔。人乳酸

using namespace GMPL;
//
// compute A^n using standard left-to-right binary method
//
template < class T > 
void expbin<T>::power (T C, const T A, const NTL::ZZ & n)
{
assign(C,A);
for (register long i = NumBits(n)-2 ; i >= 0 ; i--)
{
sqr(C, C);
if (bit(n, i) == 1)
mul(C, C, A);
}
}

经验。.CPP

#include <NTL/lzz_p.h>
#include <gmp.h>
namespace GMPL {}
using namespace GMPL;

#include "expbin.hpp"
NTL_CLIENT
int main ()
{
// NTL variables
ZZ n;
// GMP variables
mpz_t aa;
mpz_t bb;
mpz_init(aa);
mpz_init(bb);
// generate random exponent of size 512 bits
RandomLen (n, 512); // NTL function
// initialize exponentiation classes
expbin<mpz_t> obj;
// compute a^n with available methods
obj.power (bb,aa,n);
// check and output results
gmp_printf("%Zd", bb);
}

当我尝试编译EXP 时。CPP使用(如Victor Shoup的NTL在线文档所述(

g++ -g -O2 -std=c++11 -pthread -march=native exp.cpp -o t -lntl -lgmp -lm

我收到以下错误消息-

$ g++ -g -O2 -std=c++11 -pthread -march=native exp.cpp -o t -lntl -lgmp -lm
In file included from exp.cpp:8:
In file included from ./expbin.hpp:46:
./impl.hpp:16:10: warning: 'register' storage class specifier is deprecated and
incompatible with C++1z [-Wdeprecated-register]
for (register long i = NumBits(n)-2 ; i >= 0 ; i--)
^~~~~~~~~
./impl.hpp:15:5: error: no matching function for call to 'assign'
assign(C,A);
^~~~~~
exp.cpp:28:9: note: in instantiation of member function
'GMPL::expbin<__mpz_struct [1]>::power' requested here
obj.power (bb,aa,n);
^
./com.hpp:16:17: note: candidate template ignored: deduced conflicting types for
parameter 'T' ('__mpz_struct *' vs. 'const __mpz_struct *')
inline void assign(T C, const T A)
^
In file included from exp.cpp:8:
In file included from ./expbin.hpp:46:
./impl.hpp:20:13: error: no matching function for call to 'mul'
mul(C, C, A);
^~~
./com.hpp:22:10: note: candidate template ignored: deduced conflicting types for
parameter 'T' ('__mpz_struct *' vs. 'const __mpz_struct *')
void mul (T C, const T A, const T B)

关于这些错误的孜孜不倦的谷歌搜索表明,在父类中必须有一个空的构造函数,但我已经有了它。

我知道编译语句是正确的,因为除此之外,使用 NTL 时没有其他工作。在这一点上,我没有想法来解决这个问题。提前谢谢。

编辑此问题已解决。我希望这个问题被关闭或删除。

一种解决方案是不为函数包装器使用模板。似乎只有一个模板参数是有意义的(即T替换为mpz_t(,因为模板参数必须与mpz_set兼容。模板似乎是这项工作的错误工具。只需定义采用mpz_tconst mpz_t类型参数的包装器:

inline void assign(mpz_t C, const mpz_t A)
{
mpz_set(C, A);
}

您确实说过您想学习模板,但是在不适当的上下文中使用它们不会那么有帮助。而且您仍然拥有可供课程学习的模板。

话虽如此,我可以为您解释编译器消息。


警告:"寄存器"存储类说明符已弃用,并且 与 C++1z [-已弃用寄存器]
for (register long i = NumBits(n)-2 ; i >= 0 ; i--)不兼容

这是一个很容易解决的问题。摆脱该行中的"注册"。它不再是性能优化。

错误:调用"分配"assign(C,A);没有匹配函数

[...]
注意:忽略候选模板:推断出冲突的类型 参数"T">

编译器无法确定在此函数调用中用作模板参数的内容。有两个函数参数,每个参数都暗示assign的模板参数应该是什么。(这与expbin的模板参数不同,即使两者都被命名为T。当影响发生冲突时,这是一个问题。完成此操作的一种方法是指定您希望参数是什么,如

assign<T>(C, A);

这明确表明assign的模板参数(在<>之间给出(应该是expbin的模板参数(在此上下文中T的含义(。

注意:您定义的模板看起来可以正常工作,而无需像这样指定T。但是,从技术上讲,mpz_t是一个结构(而不仅仅是结构(的数组,用作参数的数组可能会衰减,这可能会导致类型混淆。

错误:调用"mul"没有匹配函数
mul(C, C, A);
注意:候选模板被忽略:推断出冲突的类型 参数"T">

相同(尽管在这种情况下有三个函数参数,因此推导出了三个T候选者(。

mul<T>(C, C, A);