简体   繁体   中英

Cannot call member function without object c++ Crypto Program

The program is supposed to be a Crypto program with a class and I am getting the error:

Crypto.cc:74:53: error: cannot call member function 'std::string Cipher::Encrypt(std::string, int)' without object

#include <iostream>
using namespace std;

class Cipher { 
        public:
                string str;
                string Encrypt(string data, int key) // First example, shifting through an array (or in our case, a string)
                {
                        string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; //Every letter in the alphabet(upper & lower case), and numbers 0-9
                        int shiftPos; //The position of the shift
                        bool shift = false; //Whether it is in our alphabet or not
                        for(int i = 0; i < data.length(); i++) //Encrypt each character in the string
                        {
                                for(int j = 0; j < chars.length(); j++) //Check each character in our char list for a match against the data string
                                        if(data[i] == chars[j]) //If we find a match
                                        {
                                                shift = true; //Set the shift for this character to true
                                                shiftPos = j; //The position in the array
                                        }
                                if(shift) //Only if it was found in our character list (chars)
                                {
                                        shiftPos += key; //Add the key to the shift position
                                        shiftPos %= chars.length(); //Modulo the value so it doesn't go out of bounds
                                        data[i] = chars[shiftPos]; //Set our new value
                                }
                                shift = false; //Set to false for next recursion
                        }
                        return data; //return the modified string
                }
                string Decrypt(string data, int key)
                {
                        string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
                        int shiftPos;
                        bool shift = false;
                        for (int i=0; i < data.length(); i++)
                        {
                                for(int j = 0; j < chars.length(); j++)
                                        if (data[i] == chars[j])
                                        {
                                                shift = true;
                                                shiftPos = j;
                                        }
                                if(shift)
                                {
                                        shiftPos -= key;
                                        shiftPos %= chars.length();
                                        data[i] = chars[shiftPos];
                                }
                                shift = false;
                        }
                        return data;



                }


};


int main()
{
    string str;
    int key = 8;
    cout << "Enter a string to encrypt: ";
    getline(cin,str);
    cout << "Encrypted: " << Cipher::Encrypt(str,key) << "\n";
        cout << "Enter a string to decrypt: ";
    getline(cin,str);
    cout << "Encrypted: " << Cipher::Decrypt(str,key) << "\n";
    return 0;
}

Thank you for the help!

您忘记了两种方法中的static关键字:

 static string Encrypt(string data, int key)

You need to call functions inside main using object of class Cipher.

IF you don't use object, then it would simply mean that those functions are not defined in the scope of main function. You need to tell that these are the part of class Cipher.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM