【问题标题】:C++, C2447 '{': missing function header (old-style format list?), win32 and longC++、C2447 '{':缺少函数头(旧式格式列表?)、win32 和 long
【发布时间】:2020-01-23 20:57:34
【问题描述】:

我在为现有的学习代码构建解决方案时遇到了错误。我定义的预处理器是 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
  • 该代码看起来有效。错误一定来自其他原因。
  • 好点添加了 3 个将显示的错误并更新了文件的完整代码,并且关于恶劣天气有点糟糕的笑话,因为添加标签时需要花费大量时间并与网站“争论”不赞成发布我认为更相关的标签,对此深表歉意。
  • 您包含的其中一个头文件中可能存在错误(例如,在类定义后缺少分号)。
  • 回复:“我还不能添加图片”——不要发布错误信息的图片。像你一样发布文本。

标签: c++ cryptography win32-process long-long error-list


【解决方案1】:

为了将来参考,您应该努力使您的示例成为一个好的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,否则标题将只是

#define countOps(n)

因此,如果我们将这两部分放在一起,我们就有了一个最小的、可重现的编译错误示例:

#define countOps(n)
static long long opsCount;
void countOps(long n){
    opsCount += n;
}

换句话说,包含的文件正试图将 #define 消除 countOps 函数,并且通过实现它,预处理器正在将您的代码变成

static long long opsCount;
void {
    opsCount += n;
}

这没有任何意义。您可以在包含CountOps.hpp 之前通过#define COUNT_OPS 解决此问题,或者干脆不费心尝试创建此功能。正确的路径取决于该库的使用方式。

我的意思不是告诉你如何解决这个问题,而是希望能帮助你了解如何在未来发现它,这样你就可以了解发生了什么,以及为什么会发生,或者至少可以帮助其他试图提供帮助的人你。

最后一点:假设您的图像永远不会被看到。不仅某些用户会看不到它们,而且将来搜索您的问题的其他用户也看不到它,因此它可以教会其他人从您的问题中学习的能力。您的编译器会在某种程度上以文本形式发出这些错误。你应该从那里复制粘贴。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 2018-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多