没有识别从头文件调用到主 C++ 的方法?

时间:2021-08-01 13:34:50

标签: c++ class methods

我在头文件中定义了一个类,并在其中声明了函数,并将函数体放在一个单独的 cpp 文件中。然后它在 main.js 中没有被识别。我已经查看了很多问题,有些在声明中使用了 static 这个词,但是当我尝试它时,函数被重新声明,所以它什么也没做,我确保头文件包含在两个 cpp 文件中,但它仍然没有认出来。

#登录和注册system.cpp#

#include <iostream>
#include <cmath>
#include <vector>
#include "L&S functions.h"
using namespace std;

int main()
{
    if (status == 1) {
        Login();   // this shows an error
        cout << "Login";
    }
    else if (status == 2) {
        Register();  // this shows an error
        cout << "Register";
    }
}

#L&S 函数.h#

#pragma once
#include <string>
using namespace std;


class User {
    string username;
    string password;
    string mail;

public:
     void Register(); 
     void Login();


};

#L&S 函数.cpp#

#include <iostream>
#include <cmath>
#include <vector>
#include "L&S functions.h"
using namespace std;

vector<User> people;                               // initiating vector "people" of type "User"

void User::Register() {
    User person;                                  // instantiating a user

    cout << "Please choose your username: ";
    cin >> User.username;                         // adds username
    cout << "\nPlease set your password: ";
    cin >> User.password;                        // adds password
    cout << "\nPlease set your email: ";
    cin >> User.mail;                           // adds email
    cout << "\nRegistered successfully!";
    people.pushback(person);                   // adds person to the vector "people"
}

void User::Login() {
    string pass;                              // takes the password typed in to check
    string name;                             // takes the username typed in to check

    cout << "Please enter your username: ";
    cin >> name;
    cout << "\nPlease enter your password: ";
    cin >> pass;

    for (int i = 0; i < people.size(); i++) {                // iterates in the people vector 

        if (name == username[i] && pass == password[i]) {    // look for a match with name and pass

            cout << "Welcome " << username[i] << "! Your email is: " << email[i];  // prints "Welcome "username"! Your email is: "email" 

        }

    }
}

2 个答案:

答案 0 :(得分:1)

错误是由于未在主文件中创建类用户的对象造成的。 您可以声明一个全局用户变量:

User g_User{};

或者你的主方法中的局部变量取决于你的用例。

另一种解决方案是将类用户中的注册和登录设为静态。

如果您选择对象方法,请执行以下操作:

g_User.Register();

答案 1 :(得分:0)

Login() 和 Register() 是“User”类的成员。如果你想使用它们,你必须创建一个 User 类的实例。所以更好的解决方案应该是让它们 staticfriend 函数。 (请注意,您在启动时接受了“状态”,如果您再次运行该应用程序,所有用户信息都将丢失。)

#include <iostream>
#include <cmath>
#include <vector>
#include "L&S functions.h"
using namespace std;

short status = 1; //defined here to avoid compilation error. Modify according to your needs

int main()
{
  if (status == 1) {
    User::Login();   // this shows an error
    cout << "Login";
  }
  else if (status == 2) {
    User::Register();  // this shows an error
    cout << "Register";
  }
}

您的头文件必须是:

#pragma once
#include <string>
using namespace std;


class User {
string username;
string password;
string mail;

public:
   static void Register();
   static void Login();
};

您的 cpp 文件是:

#include <iostream>
#include <cmath>
#include <vector>
#include "L&S functions.h"
using namespace std;

vector<User> people; 
void User::Register() 
{
   User person;  

   cout << "Please choose your username: ";
   cin >> person.username;                         // adds username
   cout << "\nPlease set your password: ";
   cin >> person.password;                        // adds password
   cout << "\nPlease set your email: ";
   cin >> person.mail;                           // adds email
   cout << "\nRegistered successfully!";
   people.push_back(person);                   // adds person to the vector "people"
 }

void User::Login() {
   string pass;                              // takes the password typed in to check
   string name;                             // takes the username typed in to check

   cout << "Please enter your username: ";
   cin >> name;
   cout << "\nPlease enter your password: ";
   cin >> pass;

   for (int i = 0; i < people.size(); i++) {                // iterates in the people vector 

    if (name == people[i].username && pass == people[i].password)
    {    
      // look for a match with name and pass

         cout << "Welcome " << people[i].username << "! Your email is: " << people[i].mail;  // prints "Welcome "username"! Your email is: "email" 

     }
   }
}