繁体   English   中英

不能在没有对象的情况下调用成员函数 c++ Crypto Program

[英]Cannot call member function without object c++ Crypto Program

该程序应该是一个带有类的加密程序,我收到错误消息:

Crypto.cc:74:53: 错误:无法调用没有对象的成员函数 'std::string Cipher::Encrypt(std::string, int)'

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

感谢您的帮助!

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

 static string Encrypt(string data, int key)

您需要使用类 Cipher 的对象在 main 中调用函数。

如果您不使用对象,则仅意味着这些函数未在 main 函数的范围内定义。 您需要说明这些是 Cipher 类的一部分。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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