C ++ [链接器错误]未定义参考

时间:2014-03-01 12:41:10

标签: c++

我正在开发一个C ++项目,我将main.cpp和两个类文件作为View和Model。所以我的主要看起来像,

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


using namespace std;

int main()
{
    View v;
    v.showLogin();


}

view.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

view.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
}

model.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

和model.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
}

因此,当我尝试编译并运行程序时,我得到了下面的恼人错误

[链接器错误]未定义引用'View :: validateLogin(std :: string,std :: string)' 我们返回1退出状态

请帮帮我

3 个答案:

答案 0 :(得分:2)

您在validateLogin课程中声明了View,但在Model课程中对其进行了定义。最简单的解决方法是删除

void validateLogin(string u, string p); 

来自View标题的行。

或将定义从Model移至View

答案 1 :(得分:1)

您应该在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();
                     }

答案 2 :(得分:1)

问题是你向编译器承诺validateLogin类中会有View函数,但你没定义。相反,你在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文件中,因为在完全声明validateLogin的接口之前,您无法调用Model

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

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