我遇到一个问题,弄清楚我在运行程序时遇到的错误是什么意思。我正在尝试做一个Caesar密码程序。还有一个头文件,其中包含大多数常量和声明的函数。
我得到的错误是:
In file included from proj1_test_public.cpp:62:
proj1.cpp: In function ‘int test_main()’:
proj1.cpp:16: error: invalid conversion from ‘char’ to ‘const char*’
proj1.cpp:16: error: initializing argument 1 of ‘char SolveCipher(const char*, char*)’
proj1.cpp:16: error: invalid conversion from ‘char’ to ‘char*’
proj1.cpp:16: error: initializing argument 2 of ‘char SolveCipher(const char*, char*)’
proj1.cpp: In function ‘void Decipher(char*, char)’:
proj1.cpp:41: warning: comparison between signed and unsigned integer expressions
proj1.cpp: In function ‘char SolveCipher(const char*, char*)’:
proj1.cpp:67: error: invalid types ‘const char[int]’ for array subscript
proj1.cpp:73: error: invalid types ‘const char[int]’ for array subscript
make: *** [proj1_test_public.o] Error 1
第16行是“char mess = SolveCipher(globeCip [i],dec_message [i]);”
第41行只是一个警告,但它有效(它在解密功能中)
第67行是“if(Decipher(cip [k] [l],keyin)== cribs [k] [l])”
第73行是“dec = Decipher(cip [k] [l],keyin);”在第二个for循环中的else语句下。
头文件代码:
#ifndef PROJ1_H
#define PROJ1_H
const int ALPHALEN = 26; // number of valid characters (A-Z)
const int MAXMSGLEN = 60; // maximum message length
const int NUMMSGS = 10; // number of messages
const int NUMCRIBS = 5; // number of cribs
const int MAXCRIBLEN = 8; // maximum crib length
const char cipher[NUMMSGS][MAXMSGLEN] = {
"HAAHJR HA KHDU AVTVYYVD",
"DHFGS NBKNBJ ZMC ZKK HR VDKK",
"Q PIDM JQMJMZ NMDMZ",
"JCTFGT DGVVGT HCUVGT UVTQPIGT",
"LRPYE I HTWW XPPE JZF LE ESP NZXXZYD",
"KLSQ GML LGG DSLW YGL FGLZAF AF EQ TJSAF",
"QEBC GUR ZVPEBSVYZ ORUVAQ GUR FGNGHR",
"GZSGZWD NX XTRJBMJWJ JQXJ FY UWJXJSY",
"RZVOCZM AJM OJHJMMJR HJMIDIB RVMH RDOC GJR XGJPYN",
"ROBO MYWO LKN XOGC DKVUSXQ DRSC KXN DRKD"
};
const char cribs[NUMCRIBS][MAXCRIBLEN] = {
"ATTACK",
"MEET",
"DROP",
"AGENT",
"WEATHER"
};
void Decipher(char cip[], char key);
char SolveCipher(const char cip[], char dec[]);
#endif
主文件的代码:
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
#include "proj1.h"
char globeCip[MAXMSGLEN];
int main()
{
char dec_message[MAXMSGLEN];
for(int i=0; i<=NUMMSGS; i++)
{
strncpy(globeCip,cipher[i],MAXMSGLEN);
char mess=SolveCipher(globeCip[i],dec_message[i]);
if(mess=='\0')
{
cout<<"Message # "<<i<<": was not interesting "<<endl;
}
else
{
cout<<"Message # "<<i<<": "<<mess<<endl;
}
}
return 0;
}
void Decipher(char cip[], char key)
{
for(int j=0; j<strlen(cip); j++)
{
if(cip[j]-key<'A')
{
char diff=(key-'A')+key;
cip[j]=(cip[j]-key)+diff;
}
else
{
cip[j]=((((cip[j]-65)-key)%26)+65);
}
}
}
char SolveCipher(const char cip[], char dec[])
{
char keyin='A';
for(int i=0; i<26; i++)
{
keyin=keyin+i;
}
for(int k=0; k<NUMMSGS; k++)
{
for(int l=0; l<MAXMSGLEN; l++)
{
if(Decipher(cip[k][l],keyin)==cribs[k][l])
{
return '\0';
}
else
{
dec=Decipher(cip[k][l],keyin);
}
}
}
}