laquishabonquiquithe3rd laquishabonquiquithe3rd - 9 months ago 27
C++ Question

I am having trouble with my basic brute force program. Specifically the classes functions

I am making a basic brute force program and i am having trouble using functions through classes.

Note-- The problem is in "Brute Force.cpp.cpp" at line 21 where i mention the classes and functions.

Brute Force.cpp.cpp(I messed up with the name)(This is the main function):

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


Hidden Password.cpp:

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


Consol:

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

Answer Source

I think you should always start debugging based on the error messages. Clearly the error is with accessing the static members as the error shows:

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'

In C++ :: is used to access static members, and NOT "regular" members. Static members are "one per class" while others are not and thus can only be accessed through objects and are copied with every object.

Thus you should use the scope resolution operator :: to access static members only, I recommend declaring the funtion int brute_force_function to static int brute_force_function.

Here are some resources for your reference: https://msdn.microsoft.com/en-us/library/b451xz31.aspx and http://www.cprogramming.com/tutorial/statickeyword.html

I noticed you declared the class as static, here are references for static classes (I wont explain myself as it is outside the scope of the problem): How do you create a static class in C++? and https://codeyarns.com/2012/01/10/c-static-class-definition/