【问题标题】:Not identifying a method called from header to main C++?没有识别从标头调用到主 C++ 的方法?
【发布时间】:2021-10-07 04:16:01
【问题描述】:

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

#登录注册系统.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" 

        }

    }
}

【问题讨论】:

  • LoginUser 对象的非静态成员函数,不是独立函数。你称它为User my_user; my_user.Login(); 之类的东西也许你的意思是让它成为一个静态函数,或者一个非成员函数。
  • 请在这种情况下逐字显示准确的错误。也就是说,您的问题是您试图调用名为LoginRegister 的“免费”函数(不在类中),但没有这样的函数。 LoginRegisterUser 类的非静态成员函数,因此您需要在调用它们之前创建一个用户对象。

标签: c++ class methods


【解决方案1】:

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" 

     }
   }
}

【讨论】:

  • 非常感谢您的帮助我试过了,但它仍然显示此错误“严重代码描述项目文件行抑制状态错误 LNK2019 未解析的外部符号”公共:静态 void __cdecl User::Register(void) " (?Register@User@@SAXXZ) 函数 main Login and registration system D:\Programming projects\C++\Login and registration system\Login and registration system\Login and registration system.obj 1中引用的"
  • 您的 L&S functions.h 和 L&S functions.cpp 在不同的项目中吗?
  • 不,他们在同一个项目中。
  • 我已经为你创建了项目,请尝试一下
  • 我运行了代码,它给了我同样的错误
【解决方案2】:

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

User g_User{};

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

另一种解决方案是在 Class User 中将 Register 和 Login 设为静态。

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

g_User.Register();

【讨论】:

  • 好的,这消除了这个错误非常感谢!但是还有另一个关于未找到链接器的错误。函数主登录和注册系统 D:\Programming projects\ C++\登录注册系统\登录注册系统\登录注册系统.obj 1 "
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多