简体   繁体   English

当我尝试编译此C ++程序时,它给了我一个错误

[英]When I try to compile this C++ program, it gives me an error

This is a program in which someone inputs a password and gets three tries to guess the password. 这是一个程序,其中有人输入密码,然后尝试进行三次猜测。 When I compile it, I get multiple errors, one of which includes line 13, where it basically says that it cannot find a similar function included in the Password_Program class. 当我编译它时,我遇到多个错误,其中一个错误包括第13行,该错误基本上说找不到在Password_Program类中包含的类似函数。

#include <iostream>
using namespace std;

class Password_Program
{
private:
     string password;
public:
     Password_Program();
     void login();
     string passAttempt;
};

Password_Program::Password_Program()
{
     cin >> password;
}
Password_Program::login()
{
     for (int i = 0; i < 3; i++)
     {
          cin >> passAttempt;
          if (passAttempt == password)
          {
               cout << "Success!" << endl;
               break;
          }
          else if (i >= 3) { cout << "Try again later" << endl;
          break; }
          else { cout << "Sorry, try again" << endl; }
     }
}

int main() {
     Password_Program myPassword;
     myPassword.login;
     return 0;
}

Your definition of the login() method is missing a return type. 您对login()方法的定义缺少返回类型。 It should be: 它应该是:

void Password_Program::login()
{
    ...
}

You've also forgotten brackets when calling it: 在调用它时,您也忘记了括号:

myPassword.login();

You forgot the return type in the definition of Password_Program::login() . 您忘记了Password_Program::login()定义中的返回类型。

Change 更改

Password_Program::login() {....}

to

void Password_Program::login() {....}

2 things are there 有2件东西

  1. You did not added the void when defining the class function login outside the class. 在类之外定义类函数登录时,没有添加空值
  2. Second that when you are calling login function you are missing () 其次,当您调用登录功能时,您会丢失()

Here is the right code. 这是正确的代码。

#include <iostream>
using namespace std;

class Password_Program
{
private:
     string password;
public:
     Password_Program();
     void login();
     string passAttempt;
};

Password_Program::Password_Program()
{
     cin >> password;
}
void Password_Program::login()
{
     for (int i = 0; i < 3; i++)
     {
          cin >> passAttempt;
          if (passAttempt == password)
          {
               cout << "Success!" << endl;
               break;
          }
          else if (i >= 3) { cout << "Try again later" << endl;
          break; }
          else { cout << "Sorry, try again" << endl; }
     }
}

int main() {
     Password_Program myPassword;
     myPassword.login();
     return 0;
}

暂无
暂无

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

相关问题 当我尝试构建 c++ 程序时出现错误 - Gives an error when i try to build a c++ program 我无法在代码块中编译 c++ 程序。 它给我错误 - I can't compile c++ program in code blocks. It gives me error 在jgrasp中编译C ++程序时,为什么会出现此错误? - Why do I get this error when I compile a C++ program in jgrasp? 我制作了这个 c++ 程序,它计算单词但是当文本输入超过 1 个单词时,它只会给我 18446744073709551615 - I made this c++ program which counts the words but when the text input is more than 1 word it just gives me 18446744073709551615 当我尝试在 C++ 中输入字符串时,程序抛出运行时错误 - When I try to input a string in C++, the program throws out a run time error 当我尝试运行程序时出现 C++ std::out_of_range 错误 - C++ std::out_of_range error when I try to run the program 当我尝试比较c ++中的两个字符串时,程序崩溃了? - Program Crashes when I try to compare two strings in c++? C ++程序给出错误“未初始化&#39;i&#39;的值” - c++ program gives an error“ the value of 'i' is not initiated” 为什么我会收到错误:当我尝试编译C ++代码时,在Eclipse中初始化&#39;Item :: Item(int)&#39;[-fpermissive]的参数1? - Why am I getting error: initializing argument 1 of 'Item::Item(int)' [-fpermissive] in Eclipse when I try to compile my C++ code? C ++给我一个错误:调用不匹配 - C++ gives me an error: no match for call to
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM