"类"在这里有一个先前的声明

'class" has a previous declaration here

本文关键字:声明 有一个 在这里      更新时间:2023-10-16

我一辈子都无法弄清楚出了什么问题。

我的制作文件:

all: main.o rsa.o
    g++ -Wall -o main bin/main.o bin/rsa.o -lcrypto
main.o: src/main.cpp inc/rsa.h
    g++ -Wall -c src/main.cpp -o bin/main.o -I inc
rsa.o: src/rsa.cpp inc/rsa.h
    g++ -Wall -c src/rsa.cpp -o bin/rsa.o -I inc

我的主要课程:

#include <iostream>
#include <stdio.h>
#include "rsa.h"
using namespace std;
int main()
{
    //RSA rsa;
    return 0;
}

我.cpp:

#include "rsa.h"
#include <iostream>
using namespace std;
RSA::RSA(){}

我的.h:

#ifndef RSA_H
#define RSA_H
class RSA
{
    RSA();
};
#endif

我收到以下错误:

In file included from src/main.cpp:7:0:
inc/rsa.h:7:7: error: using typedef-name ‘RSA’ after ‘class’
/usr/include/openssl/ossl_typ.h:140:23: error: ‘RSA’ has a previous declaration here

我觉得我已经尝试了一切,但我被卡住了。有什么想法吗?

/

usr/include/openssl/ossl_typ.h:140:23:错误:"RSA"在此处具有先前的声明

从错误消息来看,您的符号名称似乎与 OpenSSL 中定义的名为RSA的另一个类冲突。
有两种方法可以克服这个问题:

  1. 更改课程名称或
  2. 如果要
  3. 保留相同的名称,请包装在命名空间中。

编译器在 ossl_typ.h 文件中发现了 RSA 的 typedef,在编译程序时会间接 #included。我至少可以想到三种解决方案:

  1. 将类名更改为其他名称。

  2. 把你的班级放在一个namespace.

  3. 弄清楚为什么 OpenSSL 标头包含在您的构建中。环顾四周后,我发现了这个问答,上面说gcc -w -H <file>会向你展示 #included 的文件。从那里,您可以删除对OpenSSL标头的依赖。

相关文章: