所以我试图学习C ++,所以我可以学习一些逆向工程,这就是为什么我试图创建这个简单的crack me程序来获得基础,而不是在选择自己的道路时接受别人的项目。但是,我正在使用CodeBlocks,因为其他IDE并不协作,我很享受它,并且给了我一些错误和两行代码。以下是以下代码。因此,错误如下:
||=== Build: Debug in SimpleProgram (compiler: GNU GCC Compiler) ===|
D:\Programming\C++ Projects\simple programs\SimpleProgram\main.cpp||In member function 'int checker::processing(int)':|
D:\Programming\C++ Projects\simple programs\SimpleProgram\main.cpp|15|warning: no return statement in function returning non-void [-Wreturn-type]|
D:\Programming\C++ Projects\simple programs\SimpleProgram\main.cpp||In function 'int main()':|
D:\Programming\C++ Projects\simple programs\SimpleProgram\main.cpp|22|error: 'x' was not declared in this scope|
||=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
#include <iostream>
using namespace std;
class checker{
public:
int number;
processing(int x){
x = number;
if ( number == 10 ){
cout << "Well done!";
} else {
cout << "Keep trying!";
}
}
};
int main()
{
checker cracking;
cout << "Please enter in the correct number";
cin >> cracking.processing(x);
return 0;
}
函数始终具有返回类型,即使您不尝试返回任何内容,该函数也会具有void
签名。如果您打算输入从main
传来的数字,并通过类检查器中的函数通过相同的对象显示该数字,则它看起来像这样:
#include <iostream>
using namespace std;
class checker{
public:
int number;
void processing(int x)
{
if (x==10)
cout << "Well done!";
else
cout << "Keep trying!";
}
};
int main()
{
checker cracking;
cout << "Please enter in the correct number \n";
int n;
cin >> n;
cracking.processing(n);
return 0;
}
我已经清理了代码,并包含了用作注释的注释:
#include <iostream>
using namespace std;
class checker{
public:
void setnumber(int i){ //it's a good habit to put variables in private and access them with a public function
this->number = i;
};
int processing(int x){ //x is just a placeholder value for whatever you put in here. You can't use it in the rest of the program
if ( x == 10 ){
cout << "Well done!" << endl;
return 1; //this condition indicates success
} else {
cout << "Keep trying!" << endl; //the endline just makes it so you aren't typing on the same line as the message
return 0; //this condition indicates success hasn't been reached yet
}
}
private:
int number;
};
int main()
{
checker cracking;
cracking.setnumber(10); //the number is set to 10
int i, l; //i is the guess number, l is a condition for the loop
cout << "Please enter in the correct number" << endl;
do{ //this loop sets it up so that you can have multiple tries
cin >> i;
l = cracking.processing(i);
}while(l!=1); //and this condition (the return value of processing(), breaks the loop on success
return 0;
}
向我冒出的主要问题是使用x。
尝试将x设置为number
。在函数中,参数只是参数的占位符值,这些参数将在以后传递给参数。然后,当您尝试在main()
程序中使用x作为输入时。您正在调用该函数(使用它),并且需要一个int作为输入。
不用担心。在一开始,每个人都会感到困惑(虽然公平地说,随着您的进步,您会发现新事物容易混淆。它永远不会停止)。坚持下去,一切都会及时生效。