[英]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. 我正在一个C ++项目中,我的main.cpp和两个类文件分别为View和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 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 looks like 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 looks like 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
and model.cpp looks like 和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
}
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 [链接器错误]未定义对“ View :: validateLogin(std :: string,std :: string)”的引用ID返回了1个退出状态
Please help me 请帮我
You declared validateLogin
in View
class, but defined it in Model
class. 您在
View
类中声明了validateLogin
,但在Model
类中对其进行了定义。 Simplest fix is to remove the 最简单的解决方法是删除
void validateLogin(string u, string p);
line from the View
's header. View
标题中的一行。
Or move the definition from Model
to View
. 或将定义从“
Model
移动到“ View
。
You should define validateLogin
in View and not in Model. 您应该在View中而不是在Model中定义
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();
}
The problem is that you promised the compiler that there would be validateLogin
function in the View
class, but you failed to define. 问题是您向编译器承诺在
View
类中将有validateLogin
函数,但是您未能定义。 Instead, you defined one in the Model
class, which is the correct place for such function to be. 相反,您在
Model
类中定义了一个,这是该函数的正确位置。 Now you need to add a reference to Model
to your View
class, so that you could call model.validateLogin()
in your implementation file: 现在,您需要在
View
类中添加对Model
的引用,以便可以在实现文件中调用model.validateLogin()
:
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. 您将需要将
showLogin
的代码移动到cpp文件中,因为在完全声明Model
的接口之前,您无法调用validateLogin
。
Now change your main
to make a Model
before the view, and give that model to the View
's constructor: 现在,将您的
main
更改为在视图之前创建一个Model
,并将该模型提供给View
的构造函数:
int main()
{
Model m;
View v(m);
v.showLogin();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.