C++ [链接器错误] 未定义的引用

C++ [Linker Error] Undefined Reference to

本文关键字:未定义 引用 错误 链接 C++      更新时间:2023-10-16

我正在做一个C++项目,其中我有我的主.cpp和两个类文件作为视图和模型。所以我的主要看起来像,

#include <cstdlib>
#include <iostream>
#include "view.h"
#include "model.h"

using namespace std;
int main()
{
    View v;
    v.showLogin();

}

视图.h 看起来像

#ifndef VIEW_H
#define VIEW_H
#include <iostream>
#include <string>
using namespace std;
/*
 * No description
 */
class Model;
class View
{
     string username,password; 
    void validateLogin(string u, string p); 
    public:
        // class constructor
        View();
        // class destructor
        ~View();
    public: 
     void showLogin(){
         cout<< "_________________ User Login _________________"<<endl;
         cout<<'n'<<endl;
         cout<< "Enter Username : ";
         cin>>username;
         cout<< "Enter Password : ";
         cin>>password;
         validateLogin(username,password);
         //system("pause > NULL");

         }  
    public:
           void showHome(){
                cout<< "_________________ ! Welcome to AppLine ! _________________"<<endl;
                cout<<'n'<<endl;
                cout<< "1. Add a Job"<<endl;
                cout<< "2. Search a Job"<<endl;
                cout<< "2. Modify a Job"<<endl;
                system("pause > NULL");
                }     
};
#endif // VIEW_H

视图.cpp看起来像

#include "view.h" 
#include "model.h"
// class's header file
// class constructor
View::View()
{
    // insert your code here
}
// class destructor
View::~View()
{
    // insert your code here
}

模型.h看起来像

#ifndef MODEL_H
#define MODEL_H
#include <iostream>
#include <string> 
using namespace std;
/*
 * No description
 */
class View; 
class Model
{
    void showHome();  
    public:
        // class constructor
        Model();
        // class destructor
        ~Model();
    public:
           void validateLogin(string u, string p){
                if(u=="admin" && p=="1234"){
                     showHome();
                     }

                }   
};
#endif // MODEL_H

和模型.cpp看起来像

#include "model.h" // class's header file
#include "view.h"
// class constructor
Model::Model()
{
    // insert your code here
}
// class destructor
Model::~Model()
{
    // insert your code here
}

因此,当我尝试编译和运行该程序时,我收到以下烦人的错误

[链接器错误] 未定义对"查看::验证登录(std::string,std::string)"的引用ID 返回 1 个退出状态

请帮助我

您在类中声明validateLogin View但在类Model中定义了它。 最简单的解决方法是删除

void validateLogin(string u, string p); 

View标题中的行。

或者将定义从 Model 移动到 View

您应该

在视图中定义validateLogin,而不是在模型中定义。

class Model // see you are defining validateLogin in Model
{
    void showHome();  
    public:
        // class constructor
        Model();
        // class destructor
        ~Model();
    public:
           void validateLogin(string u, string p){
                if(u=="admin" && p=="1234"){
                     showHome();
                     }

问题是你答应编译器在View类中会有validateLogin函数,但你没有定义。相反,您在 Model 类中定义了一个,这是此类函数的正确位置。现在,您需要将Model的引用添加到View类中,以便可以在实现文件中调用model.validateLogin()

class View {
    string username,password;
    Model &model;
    // validateLogin is removed
public:
    // class constructor
    View(Model& m) : model(m) {};
    ...
};

您需要将showLogin的代码移动到 cpp 文件中,因为在完全声明Model 的接口之前,您无法调用validateLogin

现在更改main以在视图之前进行Model,并将该模型提供给View的构造函数:

int main()
{
    Model m;
    View v(m);
    v.showLogin();
}