我正在實施一個Caesarian密碼程序。我開始用C語言來做,因此我使用了很多標準的C函數。但是,當我意識到要加密的郵件在第一個空格之後被截斷。所以我使用了一些C++函數。需要學習:聲明'void encrypt(char *,int)'具有不同的異常說明符'
麻煩的是,當我這樣做,突然間,我得到了下面的G ++錯誤:
> 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()’
以下是該計劃的相關部分:
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++;
}
}
添加了COUT < <消息[i]位調試,以防我沒有獲得內存地址,而不是實際值。
我的問題是:這個編譯器錯誤究竟意味着什麼,它與我試圖用我的程序做什麼有關?當我不使用字符串時,這工作正常。但是,當我嘗試使用字符串輸入消息並將其轉換爲char []時,那是我得到此編譯器錯誤時。
該函數的一個簽名有'throw()',另一個沒有。 – chris
您顯示的代碼似乎無法解釋錯誤消息。你能否將第47和18行復制到你的問題中,並標記哪個是哪個? – templatetypedef
等等,你在C中的邏輯是錯誤的,所以你切換到C++。這聽起來不對。如果切換,爲什麼不使用C++提供的工具,比如讓加密函數在'std :: string'上運行而不是你目前擁有的內存泄漏? – chris