Need to be schooled: declaration ‘void encrypt(char*, int)’ has a different exception specifier'

StackOverflow https://stackoverflow.com/questions/20463445

  •  30-08-2022
  •  | 
  •  

Question

I am implementing a Caesarian cipher program. I started doing it in C, hence why I use a lot of standard C functions. However, when I realized that messages to be encrypted are truncated after the first space. So I resorted to some C++ functions.

Trouble is, when I did that, suddenly, I got the following g++ error:

> g++ -o cipher cipher.cc
cipher.cc: In function ‘void encrypt(char*, int)’:
cipher.cc:47:37: error: declaration of ‘void encrypt(char*, int)’ has a different exception specifier
cipher.cc:18:6: error: from previous declaration ‘void encrypt(char*, int) throw ()’

Here are the relevant parts of the program:

int key; // cipher key
char message[1000]; // 2^32

void encrypt(char message[], int key);
void decrypt(char message[], int key);

int main()
{
    string input;  
    cout<<"Enter your message: "<<endl;
    getline(cin,input);
    char* msgPtr = new char[input.length()+1];
    strcpy(msgPtr, input.c_str());

    for(int i=0; i<input.length()+1; i++) {
        message[i] = msgPtr[i];
        cout << message[i] << endl;
    }

//    printf("Enter message: ");
//    scanf("%s", message);

    printf("\nEnter Cipher Key: ");
    scanf("%d", &key);

    encrypt(message, key);
    printf("\nCiphertext: %s", message);

    decrypt(message, key);
    printf("\n\nDecrypted message: %s\n\n", message);

    return 0;
}

void encrypt(char message[], int key) 
{
    int i = 0;
    if((key % 94) == 0)
        key = rand() % 126+33;

    while (message[i] != '\0') {
        message[i] = message[i] + key; 
        i++;
    }
}

Added the cout << message[i] bit to debug, in case I don't get memory addresses instead of the actual values.

My question is this: what precisely does this compiler error mean and how does it relate to what I am attempting to do with my program? This works fine when I don't use a string. However, when I try to use a string to input the message and convert it to a char[], that is when I got this compiler error.

Was it helpful?

Solution

Maybe you are including some header that also declares an encrypt() function with a different signature than your local declaration.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top