undefined reference to `Caesar::Caesar(int)' among others for my Caesar Cipher Project

143 Views Asked by At

I have written a .cpp file to encrypt and decrypt strings passed to it. The problem that I am having is that unless I include the Caesar.cpp file (my solution) in the test.cpp file, then the test file produces undefined reference errors. When I did include the Caesar.cpp file in the TestCaesar.cpp file as #include "Caesar.cpp", the test ran perfectly. I am using Visual Studio Code with GCC on Windows as my compiler. I can't edit anything about the header file (part of the assignment), but I will add that and the other files I've mentioned to this post. My header file:

// 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

My Caesar.cpp file:

#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
}

My TestCaesar.cpp file:

#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;
}
0

There are 0 best solutions below