我的基本蛮力计划遇到了麻烦。特别是类的功能

时间:2017-03-12 05:48:19

标签: c++ visual-studio class

我正在制作一个基本的暴力程序,我在通过课程使用函数时遇到了麻烦。

注意 - 问题出在" Brute Force.cpp.cpp"在第21行,我提到了类和函数。

Brute Force.cpp.cpp(我搞砸了名字)(这是主要功能):

#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <string>

#include "Hidden Password.cpp"
#include "Brute Force.cpp"

using namespace std;

int main()
{
    string bruteforce;


    cout << "Enter the name to brute force \n";

    Hidden_Password::Password_Function(brute_force::brute_force_function());
    return 0;
}

Brute Force.cpp:

#pragma once
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <string>

using namespace std;

static class brute_force {
    int input;

    public:

        int brute_force_function() {
            for (int i = 0; i >= 10000000000;i++) {
                return i;
            }
        }
    };

隐藏密码.cc:

#pragma once 
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <string>

#include "Hidden Password.cpp"

using namespace std;

    static class Hidden_Password {
    int private_password = 9265832594;
    int input_password;

    bool correct_password = true;

    public:

            //function
            int Password_Function(int input_password) {
                while (bool correct_password = true) {
                    if (input_password == private_password) {
                        bool correct_password = false;
                        cout << "You got the password \n";
                    }
                }
            }
};

康寿:

1>------ Build started: Project: Brute Force.cpp, Configuration: Debug Win32 ------
1>Hidden Password.cpp
1>c:\users\lisa\documents\visual studio 2017\projects\brute force.cpp\brute force.cpp\hidden password.cpp(15): warning C4305: 'initializing': truncation from '__int64' to 'int'
1>c:\users\lisa\documents\visual studio 2017\projects\brute force.cpp\brute force.cpp\hidden password.cpp(15): warning C4309: 'initializing': truncation of constant value
1>c:\users\lisa\documents\visual studio 2017\projects\brute force.cpp\brute force.cpp\hidden password.cpp(31): warning C4091: 'static ': ignored on left of 'Hidden_Password' when no variable is declared
1>c:\users\lisa\documents\visual studio 2017\projects\brute force.cpp\brute force.cpp\hidden password.cpp(14): error C2011: 'Hidden_Password': 'class' type redefinition
1>c:\users\lisa\documents\visual studio 2017\projects\brute force.cpp\brute force.cpp\hidden password.cpp(14): note: see declaration of 'Hidden_Password'
1>Brute Force.cpp.cpp
1>c:\users\lisa\documents\visual studio 2017\projects\brute force.cpp\brute force.cpp\hidden password.cpp(15): warning C4305: 'initializing': truncation from '__int64' to 'int'
1>c:\users\lisa\documents\visual studio 2017\projects\brute force.cpp\brute force.cpp\hidden password.cpp(15): warning C4309: 'initializing': truncation of constant value
1>c:\users\lisa\documents\visual studio 2017\projects\brute force.cpp\brute force.cpp\hidden password.cpp(31): warning C4091: 'static ': ignored on left of 'Hidden_Password' when no variable is declared
1>c:\users\lisa\documents\visual studio 2017\projects\brute force.cpp\brute force.cpp\brute force.cpp(22): warning C4091: 'static ': ignored on left of 'brute_force' when no variable is declared
1>c:\users\lisa\documents\visual studio 2017\projects\brute force.cpp\brute force.cpp\brute force.cpp.cpp(23): error C2352: 'brute_force::brute_force_function': illegal call of non-static member function
1>c:\users\lisa\documents\visual studio 2017\projects\brute force.cpp\brute force.cpp\brute force.cpp(17): note: see declaration of 'brute_force::brute_force_function'
1>Generating Code...
1>Done building project "Brute Force.cpp.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

2 个答案:

答案 0 :(得分:2)

我认为您应该始终根据错误消息开始调试。显然错误是访问静态成员,如错误所示:

  

1&gt; c:\ users \ lisa \ documents \ visual studio 2017 \ projects \ brute   force.cpp \ brute force.cpp \ brute force.cpp.cpp(23):错误C2352:   'brute_force :: brute_force_function':非静态成员的非法调用   功能1&gt; c:\ users \ lisa \ documents \ visual studio 2017 \ projects \ brute   force.cpp \ brute force.cpp \ brute force.cpp(17):注意:见声明   'brute_force :: brute_force_function'

在C ++中,::用于访问静态成员,而不是“常规”成员。静态成员是“每个类一个”,而其他成员则不是,因此只能通过对象访问,并随每个对象一起复制。

因此,您应该使用范围解析运算符::来仅访问静态成员,我建议将int brute_force_function声明为static int brute_force_function

以下是一些供您参考的资源:https://msdn.microsoft.com/en-us/library/b451xz31.aspxhttp://www.cprogramming.com/tutorial/statickeyword.html

我注意到你将类声明为静态,这里是静态类的引用(我不解释自己,因为它超出了问题的范围):How do you create a static class in C++?https://codeyarns.com/2012/01/10/c-static-class-definition/

答案 1 :(得分:1)

要做的事情:

  1. 只有当函数为$v = $_GET['v']; if($v == 1){ header('location: YOUR LINK'); die; } elseif($v == 2){ header('location: YOUR LINK'); die; } elseif($v==3){ header('location: YOUR LINK'); die; } else{ header('location:DEFAULT LINK'); die; } 时,范围解析操作::才能像brute_force::brute_force_function()一样使用。
  2. 替换static

    int brute_force_function() {

    1. 为什么static int brute_force_function() {中的#include Hidden Password.cpp

    2. Hidden Password.cpp重命名为Brute Force.cpp.cpp