我在基于现有学习代码构建解决方案时遇到错误。我定义的预处理器是 Win32(应该是),代码本身来自 https://www.nayuki.io/page/bitcoin-cryptography-library .该错误似乎是由 long
值创建的,该值可能是导致此错误的原因(使用的是 win32 而不是 win64)。我试图克服以不同方式编写它(使用 long
/ulong
/int
)但没有成功。
static long long opsCount;
void countOps(long n){
opsCount += n;
}
有什么建议可以用其他方式编写并解决错误列表中的恶劣天气吗?
/*
* A runnable main program that calculates and prints the approximate
* number of 32-bit arithmetic operations needed to perform
* elliptic curve point multiplication, in this C++ implementation.
*
* Bitcoin cryptography library
* Copyright (c) Project Nayuki
*
* https://www.nayuki.io/page/bitcoin-cryptography-library
* https://github.com/nayuki/Bitcoin-Cryptography-Library
*/
#include <cstddef>
#include <cstdlib>
#include <iostream>
#include <string>
#include "CountOps.hpp"
#include "CurvePoint.hpp"
#include "Ecdsa.hpp"
#include "FieldInt.hpp"
#include "Sha256.hpp"
#include "Sha256Hash.hpp"
#include "Uint256.hpp"
static long long opsCount;
void countOps(long n){
opsCount += n;
}
static void printOps(const char *name);
static void doUint256();
static void doFieldInt();
static void doCurvePoint();
static void doEcdsa();
int main() {
doUint256();
doFieldInt();
doCurvePoint();
doEcdsa();
return EXIT_SUCCESS;
}
static void doUint256() {
{
Uint256 x = Uint256::ONE;
Uint256 y = Uint256::ONE;
opsCount = 0;
x.replace(y, 1);
printOps("uiReplace");
}
{
Uint256 x = Uint256::ONE;
Uint256 y = Uint256::ONE;
opsCount = 0;
x.swap(y, 1);
printOps("uiSwap");
}
{
Uint256 x = Uint256::ONE;
Uint256 y = Uint256::ONE;
opsCount = 0;
x == y;
printOps("uiEquals");
}
{
Uint256 x = Uint256::ONE;
Uint256 y = Uint256::ONE;
opsCount = 0;
x < y;
printOps("uiLessThan");
}
{
Uint256 x = Uint256::ONE;
Uint256 y = Uint256::ONE;
opsCount = 0;
x.add(y);
printOps("uiAdd");
}
{
Uint256 x = Uint256::ONE;
Uint256 y = Uint256::ONE;
opsCount = 0;
x.subtract(y);
printOps("uiSubtract");
}
{
Uint256 x = Uint256::ONE;
opsCount = 0;
x.shiftLeft1();
printOps("uiShiftLeft1");
}
{
Uint256 x = Uint256::ONE;
opsCount = 0;
x.shiftRight1();
printOps("uiShiftRight1");
}
{
Uint256 x = Uint256::ONE;
Uint256 y = CurvePoint::ORDER;
opsCount = 0;
x.reciprocal(y);
printOps("uiReciprocal");
}
std::cout << std::endl;
}
static void doFieldInt() {
{
FieldInt x(Uint256::ONE);
FieldInt y(Uint256::ONE);
opsCount = 0;
x.replace(y, 1);
printOps("fiReplace");
}
{
FieldInt x(Uint256::ONE);
FieldInt y(Uint256::ONE);
opsCount = 0;
x == y;
printOps("fiEquals");
}
{
FieldInt x(Uint256::ONE);
FieldInt y(Uint256::ONE);
opsCount = 0;
x < y;
printOps("fiLessThan");
}
{
FieldInt x(Uint256::ONE);
FieldInt y(Uint256::ONE);
opsCount = 0;
x.add(y);
printOps("fiAdd");
}
{
FieldInt x(Uint256::ONE);
FieldInt y(Uint256::ONE);
opsCount = 0;
x.subtract(y);
printOps("fiSubtract");
}
{
FieldInt x(Uint256::ONE);
opsCount = 0;
x.multiply2();
printOps("fiMultiply2");
}
{
FieldInt x(Uint256::ONE);
FieldInt y(Uint256::ONE);
opsCount = 0;
x.multiply(y);
printOps("fiMultiply");
}
{
FieldInt x(Uint256::ONE);
opsCount = 0;
x.square();
printOps("fiSquare");
}
{
FieldInt x(Uint256::ONE);
opsCount = 0;
x.reciprocal();
printOps("fiReciprocal");
}
std::cout << std::endl;
}
static void doCurvePoint() {
{
CurvePoint x = CurvePoint::G;
CurvePoint y = CurvePoint::G;
opsCount = 0;
x.replace(y, 1);
printOps("cpReplace");
}
{
CurvePoint x = CurvePoint::G;
opsCount = 0;
x.isZero();
printOps("cpIsZero");
}
{
CurvePoint x = CurvePoint::G;
CurvePoint y = CurvePoint::G;
opsCount = 0;
x == y;
printOps("cpEquals");
}
{
CurvePoint x = CurvePoint::G;
opsCount = 0;
x.twice();
printOps("cpTwice");
}
{
CurvePoint x = CurvePoint::G;
CurvePoint y = CurvePoint::G;
opsCount = 0;
x.add(y);
printOps("cpAdd");
}
{
CurvePoint x = CurvePoint::G;
Uint256 y = Uint256::ONE;
opsCount = 0;
x.multiply(y);
printOps("cpMultiply");
}
{
CurvePoint x = CurvePoint::G;
opsCount = 0;
x.normalize();
printOps("cpNormalize");
}
{
CurvePoint x = CurvePoint::G;
opsCount = 0;
x.isOnCurve();
printOps("cpIsOnCurve");
}
std::cout << std::endl;
}
static void doEcdsa() {
{
Uint256 privKey = Uint256::ONE;
Sha256Hash msgHash = Sha256::getHash(nullptr, 0);
Uint256 nonce = Uint256::ONE;
Uint256 outR, outS;
opsCount = 0;
Ecdsa::sign(privKey, msgHash, nonce, outR, outS);
printOps("edSign");
}
{
CurvePoint pubKey = CurvePoint::G;
Sha256Hash msgHash = Sha256::getHash(nullptr, 0);
Uint256 r = Uint256::ONE;
Uint256 s = Uint256::ONE;
opsCount = 0;
Ecdsa::verify(pubKey, msgHash, r, s);
printOps("edVerify");
}
std::cout << std::endl;
}
static void printOps(const char *name) {
std::string s = std::to_string(opsCount);
while (s.size() < 9)
s.insert(0, " ", 1);
for (std::size_t i = s.size(); i >= 4; i -= 3)
s.insert(i - 3, " ", 1);
std::cout << s << " " << name << std::endl;
}
错误如下:
最佳答案
为了将来引用,您应该努力使您的示例成为一个好的 Minimal, Reproducible Example .这不仅会增加您在 Stack Overflow 上获得回复的机会,而且很可能会成功,因此您可以开始自己解决问题。
例如,这是您遇到问题的单个函数:
#define countOps(n)
static long long opsCount;
void countOps(long n){
opsCount += n;
}
如果您尝试在 .cpp
文件中编译它,它会工作,但会提示缺少入口点。所以,#include
一定是导致问题的原因。明显的候选者是 CountOps.hpp
声明它的地方。在这种情况下,除非您 #define
COUNT_OPS
,否则 header 将只是
#define countOps(n)
因此,如果我们将这两部分放在一起,我们就会得到一个最小的、可重现的编译错误示例:
#define countOps(n)
static long long opsCount;
void countOps(long n){
opsCount += n;
}
换句话说,包含的文件试图将 countOps 函数#define
置为空,通过实现它,预处理器将您的代码转换为
static long long opsCount;
void {
opsCount += n;
}
这毫无意义。您可以在包含 CountOps.hpp
之前通过 #define COUNT_OPS
解决此问题,或者根本不费心尝试创建此函数。正确的路径取决于该库的使用方式。
我的意思不是要告诉您如何解决这个问题,而是希望能帮助您了解将来如何检测它,这样您就可以了解正在发生的事情以及为什么会发生,或者至少帮助其他试图提供帮助的人你。
最后一点:假设您的图像永远不会被看到。不仅某些用户看不到它们,而且将来搜索您的问题的其他用户也看不到它,因此它教训了其他人从您的问题中学习的能力。您的编译器将在某种程度上在文本中发出这些错误。您应该从那里复制粘贴。
关于C++、C2447 '{' : missing function header (old-style format list? )、win32 和 long,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58068362/