Dilukshan Mahendra
Dilukshan Mahendra

Reputation: 3398

C++ [Linker Error] Undefined Reference to

I am working on a C++ project where I have my main.cpp and two class files as View and Model. So my main looks like,

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


using namespace std;

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


}

view.h looks like

#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 looks like

#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 looks like

#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

and model.cpp looks like

#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
}

So while I'm trying to compile and run the program I get the annoying error below

[Linker Error] Undefined Reference to 'View::validateLogin(std::string,std::string)' Id returned 1 exit status

Please help me

Upvotes: 0

Views: 893

Answers (3)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726579

The problem is that you promised the compiler that there would be validateLogin function in the View class, but you failed to define. Instead, you defined one in the Model class, which is the correct place for such function to be. Now you need to add a reference to Model to your View class, so that you could call model.validateLogin() in your implementation file:

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

You will need to move the code of showLogin into cpp file, because you cannot call validateLogin until Model's interface is fully declared.

Now change your main to make a Model before the view, and give that model to the View's constructor:

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

Upvotes: 1

Pranit Kothari
Pranit Kothari

Reputation: 9841

You should define validateLogin in View and not in Model.

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();
                     }

Upvotes: 1

BartoszKP
BartoszKP

Reputation: 35891

You declared validateLogin in View class, but defined it in Model class. Simplest fix is to remove the

void validateLogin(string u, string p); 

line from the View's header.

Or move the definition from Model to View.

Upvotes: 2

Related Questions