问题描述
我写了一个.cpp文件来加密和解密传递给它的字符串。我遇到的问题是,除非在test.cpp文件中包含Caesar.cpp文件(我的解决方案),否则测试文件会产生未定义的引用错误。当我确实将Caesar.cpp文件作为#include“ Caesar.cpp”包含在TestCaesar.cpp文件中时,测试运行完美。我在Windows上的GCC上使用Visual Studio Code作为编译器。我无法编辑有关头文件的任何内容(分配的一部分),但是我将添加该内容以及我在本文中提到的其他文件。 我的头文件:
// include file for Caesar cipher code
//
#ifndef CAESAR_H
#define CAESAR_H
#include <string>
class Caesar {
private:
//pointers used to refer to the standard alphabet array and the Caesar shift array
char* std_alphabet;
char* c_alphabet;
public:
// The constructor . . .
// create the two arrays with the c_alphabet array contents representing the std_alphabet
// characters shifted by a value of the shift parameter
Caesar(int shift = 0);
// encrypt a message. Returns count of characters processed
// first string is message,second is encrypted string
int encrypt(const std::string& message,std::string& emessage);
// decrypt a message. Returns count of characters processed
// first string is encrypted string,second is decrypted string
int decrypt(const std::string& message,std::string& dmessage);
//delete any memory resources used by the class
~Caesar();
}; // end of class . . .
#endif
我的Caesar.cpp文件:
#include "Caesar.h" // provides Caesar
#include <string>
Caesar::Caesar(int shift) // constructor
{
const std::string alpha1("abcdefghijklmnopqrstuvwxyz"); // assign full alphabet
std_alphabet = new char[26]; // allocate char array storage to std_alphabet
c_alphabet = new char[26]; // allocate char array storage to c_alphabet
for (int i = 0; i < 26; i++) // iterate from 0 to 25
{
std_alphabet[i] = alpha1[i]; // assign the alphabet to the std_alphabet array
c_alphabet[i] = char(int(alpha1[i] + shift - 97) % 26 + 97); // assign the shifted alphabet to c_alphabet
}
}
Caesar::~Caesar() // destructor
{
delete[] std_alphabet; // deletes the created array std_alphabet
delete[] c_alphabet; // deletes the created array c_alphabet
}
int Caesar::encrypt(const std::string& message,std::string& emessage) // encrypts the encrypted message and assigns it to emessage
{
int convertLetters = 0; // initialize count of converted letters to 0
for (int i = 0; i < message.length(); i++) // iterate the length (number of characters) of the message minus one
{
if (isalpha(message[i])){ // check if the character is alphabetic
char c1 = tolower(message[i]); // assign the lowercase version of the character to a variable
for (int j=0; j < 26; j++) // iterate from 0 to 25
{
if (c1 == std_alphabet[j]) // check if the assigned variable matches the letter of the alphabet corresponding with the current value of j
{
emessage[i] = c_alphabet[j]; // assign the matched letter to a position in the emessage array,encrypting that letter
}
}
convertLetters++; // iterate the number of converted letters
}
else // if the character is not alphabetic,this else statement runs
{
emessage[i] += message[i]; // assigns non-alphabetic characters to the emessage array
}
}
return convertLetters; // returns the number of converted letters
}
int Caesar::decrypt(const std::string& message,std::string& dmessage) // decrypts the encrypted message and assigns it to dmessage
{
int convertLetters = 0; // initialize count of converted letters to 0
for (int i = 0; i < message.length(); i++) // iterate the length (number of characters) of the message minus one
{
if (isalpha(message[i])) // check if the character is alphabetic
{
char c2 = tolower(message[i]); // assign the lowercase version of the character to a variable
int j = 0; // initializes the variable j to 0
while (c2 != c_alphabet[j]) // checks if the c2 variable is not equal to letters of the shifted alphabet,if it is equal,then that is the value of the decrypted letter in the other alphabet
{
j++; // iterates j
}
dmessage[i] = std_alphabet[j]; // assigns the decrypted letter to the dmessage array
convertLetters++; // iterates the number of converted letters
}
else // if the character is not alphabetic,this else statement runs
{
dmessage[i] = message[i]; // assigns non-alphabetic characters to the dmessage array
}
}
return convertLetters; // returns the number of converted letters
}
我的TestCaesar.cpp文件:
#include "Caesar.h"
#include <iostream>
#include <string>
int main()
{
Caesar c1(1);
Caesar c2(2);
Caesar c3(3);
Caesar c4(4);
Caesar c5(5);
Caesar c6(6);
Caesar c7(7);
Caesar c8(8);
Caesar c9(9);
Caesar c10(10);
std::string j = " ",k = " ",l = " ",m = "";
std::cout << c1.encrypt("Have",j);
std::cout << " " << std::endl;
for (int n = 0; n < 4; n++)
{
std::cout << j[n];
}
std::cout << " " << std::endl;
std::cout << c2.decrypt("Have a day!",k);
std::cout << " " << std::endl;
for (int o = 0; o < 11; o++)
{
std::cout << k[o];
}
std::cout << " " << std::endl;
std::cout << c3.encrypt("Going well",l);
std::cout << " " << std::endl;
for (int p = 0; p < 10; p++)
{
std::cout << l[p];
}
std::cout << " " << std::endl;
std::cout << c10.encrypt("Going well",m);
std::cout << " " << std::endl;
for (int q = 0; q < 10; q++)
{
std::cout << m[q];
}
std::cout << " " << std::endl;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)