gymnerd
gymnerd

Reputation: 27

'class" has a previous declaration here

I cannot for the life of me figure out what's wrong.

My makefile:

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

My main class:

#include <iostream>
#include <stdio.h>
#include "rsa.h"

using namespace std;
int main()
{
    //RSA rsa;
    return 0;
}

My .cpp:

#include "rsa.h"
#include <iostream>
using namespace std;

RSA::RSA(){}

My .h:

#ifndef RSA_H
#define RSA_H

class RSA
{
    RSA();
};
#endif

I'm getting the following error:

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

I feel like I've tried everything but I'm stuck. Any ideas?

Upvotes: 0

Views: 3160

Answers (2)

Code-Apprentice
Code-Apprentice

Reputation: 83557

Your compiler found a typedef for RSA in the ossl_typ.h file, which is indirectly #included when you compile your program. I can think of at least three solutions:

  1. Change your class name to something else.

  2. Put your class in a namespace.

  3. Figure out why the OpenSSL header is included in your build. After looking around, I found this Q&A which says that gcc -w -H <file> will show you the files which are #included. From there you might be able to remove the dependency on the OpenSSL headers.

Upvotes: 1

Alok Save
Alok Save

Reputation: 206566

/usr/include/openssl/ossl_typ.h:140:23: error: ‘RSA’ has a previous declaration here

From the error message it seems that You have a symbol name clash with another class named RSA defined inside OpenSSL.
There are two ways to overcome the problem:

  1. Change your class name or
  2. Wrap up in a namespace if you want to keep the same name.

Upvotes: 4

Related Questions