Connect main.cpp, functions.h and functions.cpp

Hello, I want my functions to be declared in functions.h, I want their bodies to be in functions.cpp and I have my main.cpp, which calls these functions. How to connect them properly? When I try to run main.cpp, I get an error:


1
2
3
4
5
6
7
8
C:\Users\nekit\AppData\Local\Temp\ccJLMW6F.o:main.cpp:(.text.startup+0x3db): undefined reference to `makeUpper(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
C:\Users\nekit\AppData\Local\Temp\ccJLMW6F.o:main.cpp:(.text.startup+0x45d): undefined reference to `makeUpper(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
C:\Users\nekit\AppData\Local\Temp\ccJLMW6F.o:main.cpp:(.text.startup+0x4df): undefined reference to `spaceEraser(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
C:\Users\nekit\AppData\Local\Temp\ccJLMW6F.o:main.cpp:(.text.startup+0x561): undefined reference to `spaceEraser(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
C:\Users\nekit\AppData\Local\Temp\ccJLMW6F.o:main.cpp:(.text.startup+0x5fe): undefined reference to `newKeyGen(int, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
C:\Users\nekit\AppData\Local\Temp\ccJLMW6F.o:main.cpp:(.text.startup+0x696): undefined reference to `encryptor(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
C:\Users\nekit\AppData\Local\Temp\ccJLMW6F.o:main.cpp:(.text.startup+0x746): undefined reference to `decryptor(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2.exe: error: ld returned 1 exit status 



This is my main.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<cctype>    
#include<iostream>
#include <iomanip>
#include <limits>
#include <fstream>
#include <string>
#include "functions.h"
using namespace std;

int main (int argc, char* argv[]) {
    
        for (int i = 0; i < argc; i++) {
        string temp = argv[i];
        if (temp == "--en") {
            cout << "--en";
            break;
        } else if (temp == "--de") {
            cout << "de" << endl;
        } else if (temp == "--br") {
            cout << "br" << endl;
        }
    }
 
    // WITH TEXT FILES
    ifstream infile("text.txt"); // defining a file variable
    ofstream outfile;
    if (!infile) {
        cout << "File text.txt has not been opened!";
        return 1;
    }
    string inputMsg, inputKey;
    while(!infile.eof()) {
        getline(infile, inputMsg); 
        getline(infile, inputKey); // assigning values to x and y from text file
    };
    infile.close(); // closing text file

    // INITILIAZING VARIABLES
    string msg = inputMsg;
    string key = inputKey;
    msg = makeUpper(msg);
    key = makeUpper(key);
    msg = spaceEraser(msg);
    key = spaceEraser(key);
    

    int msgLen = msg.length(), keyLen = key.length();

    // CALLING FUNCTIONS
    string newKey = newKeyGen(msgLen, keyLen, key);
    string encryptedMsg = encryptor(msgLen, msg, newKey);
    string decryptedMsg = decryptor(msgLen, encryptedMsg, newKey);

    newKey += '\0';
    // decryptedMsg += '\0';
 
    // PRINTING RESULTS
    cout << "Original Message: " << msg;
    cout << "\nKey: " << key;
    cout << "\nNew Generated Key: " <<newKey;
    cout << "\nEncrypted Message: " << encryptedMsg;
    cout << "\nDecrypted Message: " << decryptedMsg;

    outfile.open("text.txt");
    if (!outfile) {
        cout << "File text.txt has not been opened!";
        return 1;
    }
    outfile << encryptedMsg << endl;
	return 0;
}



This is my functions.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#pragma once
#include <cctype>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

string decryptor(int msgLen, string encryptedMsg, string newKey);


string encryptor(int msgLen, string msg, string newKey);


string newKeyGen(int msgLen, int keyLen, string key);

string makeUpper(string x);


string spaceEraser(string x);


Finally, this is my functions.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "functions.h"
#include <cctype>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

string decryptor(int msgLen, string encryptedMsg, string newKey)
{
    string decryptedMsg = "";
    for (int i = 0; i < msgLen; ++i)
    {
        decryptedMsg += (((encryptedMsg[i] - newKey[i]) + 26) % 26) + 'A';
    }
    return decryptedMsg;
}

string encryptor(int msgLen, string msg, string newKey)
{
    string encryptedMsg = "";
    for (int i = 0; i < msgLen; i++)
    {
        encryptedMsg += ((msg[i] + newKey[i]) % 26) + 'A';
    }
    encryptedMsg += '\0';
    return encryptedMsg;
}

string newKeyGen(int msgLen, int keyLen, string key)
{
    string newKey = "";
    for (int i = 0, j = 0; i < msgLen; ++i, ++j)
    {
        if (j == keyLen)
        {
            j = 0;
        }
        newKey += key[j];
    }
    return newKey;
}

string makeUpper(string x)
{
    char temp;
    for (int i = 0; i < x.length(); i++)
    {
        temp = x[i];
        temp = toupper(temp);
        x[i] = temp;
    }
    return x;
}

string spaceEraser(string x)
{
    x.erase(remove(x.begin(), x.end(), ' '), x.end());
    return x;
}
$ g++ -c main.cpp
$ g++ -c functions.cpp
$ g++ main.o functions.o -o program.bin


the .h just need to be #include where needed
Topic archived. No new replies allowed.