在c++中通过函数给动态bitset赋位
Assiging bits to dynamic bitset via function in C++
这段代码使用了类,类rsa有一个函数集,它应该接受3个整数和1个动态bitset。然而,编译器返回错误,我认为他们都是关于同一件事:
damage.cc: In function ‘int main()’:
damage.cc:81:27: error: no matching function for call to ‘public_key::set(int, int, int, int)’
usr1.set (11, 5, 23, 00001);
^
damage.cc:81:27: note: candidate is:
damage.cc:59:6: note: void rsa::set(int, int, int, const boost::dynamic_bitset<>&)
void rsa::set(int p_, int q_, int d_, const boost::dynamic_bitset <>& m_ )
^
damage.cc:59:6: note: no known conversion for argument 4 from ‘int’ to ‘const boost::dynamic_bitset<>&’
damage.cc:82:27: error: no matching function for call to ‘public_key::set(int, int, int, int)’
usr2.set (13, 7, 97, 00010);
^
damage.cc:82:27: note: candidate is:
damage.cc:59:6: note: void rsa::set(int, int, int, const boost::dynamic_bitset<>&)
void rsa::set(int p_, int q_, int d_, const boost::dynamic_bitset <>& m_ )
^
damage.cc:59:6: note: no known conversion for argument 4 from ‘int’ to ‘const boost::dynamic_bitset<>&’
damage.cc:83:29: error: no matching function for call to ‘public_key::set(int, int, int, int)’
usr3.set (11, 17, 997, 00011);
^
damage.cc:83:29: note: candidate is:
damage.cc:59:6: note: void rsa::set(int, int, int, const boost::dynamic_bitset<>&)
void rsa::set(int p_, int q_, int d_, const boost::dynamic_bitset <>& m_ )
^
damage.cc:59:6: note: no known conversion for argument 4 from ‘int’ to ‘const boost::dynamic_bitset<>&’
damage.cc:84:28: error: no matching function for call to ‘private_key::set(int, int, int, int)’
usr1r.set (17, 7, 51, 10011);
^
damage.cc:84:28: note: candidate is:
damage.cc:59:6: note: void rsa::set(int, int, int, const boost::dynamic_bitset<>&)
void rsa::set(int p_, int q_, int d_, const boost::dynamic_bitset <>& m_ )
^
damage.cc:59:6: note: no known conversion for argument 4 from ‘int’ to ‘const boost::dynamic_bitset<>&’
damage.cc:85:29: error: no matching function for call to ‘private_key::set(int, int, int, int)’
usr2r.set (11, 17, 51, 10110);
^
damage.cc:85:29: note: candidate is:
damage.cc:59:6: note: void rsa::set(int, int, int, const boost::dynamic_bitset<>&)
void rsa::set(int p_, int q_, int d_, const boost::dynamic_bitset <>& m_ )
^
damage.cc:59:6: note: no known conversion for argument 4 from ‘int’ to ‘const boost::dynamic_bitset<>&’
代码如下:
#include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
#include <boost/dynamic_bitset.hpp>
using namespace std;
class rsa {
protected:
int polyLoc, x, y, p, q, d, m, n, f, e, c, end, k;
boost::dynamic_bitset<> inpSeq;
boost::dynamic_bitset<> operSeq;
boost::dynamic_bitset<> bit;
vector <int> xorArray;
vector <int> keyReg;
public:
rsa () : polyLoc(3210), inpSeq(5), operSeq(5), bit(5), x(0), y(0), n(0), e(0), c(0), k(0), end(0), f(0) {};
void set (int , int , int, const boost::dynamic_bitset <>& );
int key () {
while(polyLoc>0)
{
xorArray.push_back(polyLoc%10);
polyLoc/=10;
}
sort(xorArray.rbegin(), xorArray.rend());
operSeq = inpSeq;
keyReg.push_back(inpSeq[0]);
x = xorArray[0];
do {
for (unsigned int r = 1; r < xorArray.size(); r++)
{
bit[4] = operSeq[x];
y = xorArray[r];
bit[4] = bit[4] ^ operSeq[y];
}
operSeq >>= 1;
operSeq[4] = bit[4];
keyReg.push_back(operSeq[0]);
}
while ((operSeq != inpSeq));
for ( unsigned int i = 0; i < keyReg.size(); i++)
{
if (keyReg[i]==1)
m = m + int(pow(2,i));
}
n = p*q;
f = (p-1)*(q-1);
for (k ; end < 1; k++)
{
if ((1+k*f)%d==0)
{
end = 2;
e = (1+k*f)/d;
}
}
c = int(pow(m,e))%n;
return c;}
};
void rsa::set(int p_, int q_, int d_, const boost::dynamic_bitset <>& m_ )
{
p = p_;
q = q_;
d = d_;
inpSeq = m_;
}
class public_key : public rsa {
public:
public_key () : rsa () {} ;
};
class private_key : public rsa {
public:
private_key () : rsa () {} ;
};
int main()
{
public_key usr1, usr2, usr3;
private_key usr1r, usr2r, usr3r;
usr1.set (11, 5, 23, 00001);
usr2.set (13, 7, 97, 00010);
usr3.set (11, 17, 997, 00011);
usr1r.set (17, 7, 51, 10011);
usr2r.set (11, 17, 51, 10110);
cout << "Public key of user 1: " << usr1.key() << endl;
cout << "Public key of user 2: " << usr2.key() << endl;
cout << "Public key of user 3: " << usr3.key() << endl;
cin.get();
return 0;
}
您必须正确构建dynamic_bitset
。
usr1.set (11, 5, 23, boost::dynamic_bitset<>(std::string("00001")));
对调用set
的main()函数中的每一行执行此操作。
这个变化的原因是dynamic_bitset
类有一个接受std::string的构造函数。由于它被声明为显式的,所以必须发送std::string,而不是字符串字面值或字符数组。
boost::dynamic_bitset
的构造函数是显式的。因此,必须显式地构造第四个参数,如boost::dynamic_bitset<>(12345)
。
相关文章:
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- C++中的动态铸造故障
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 内联映射初始化的动态atexit析构函数崩溃
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 控制允许动态运行c++的并发操作数
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 在调用FreeLibrary后,释放动态链接到具有相同版本的CRT堆的DLL的内存
- 输出没有重复元素的动态数组(收缩数组)C++
- C++为线程工作动态地分割例程
- 正在插入动态数组
- 在c++中使用动态分配的问题
- C++中的动态对象与非动态对象
- 如何在动态数组上使用搜索函数
- 视觉studo 2019中的漫画和静态/动态绑定
- 从C++中的数字输入动态创建矩阵
- 在c++中通过函数给动态bitset赋位
- 在c++类中声明动态bitset并为其赋值
- 在标准c++中创建一个动态bitset