C++ [链接器错误] 未定义的引用
C++ [Linker Error] Undefined Reference to
我正在做一个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();
}
相关文章:
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 对 Scalar ::Scalar() 的未定义引用
- 对复制 CTOR 和 CTOR 的未定义引用
- 对显式实例化的模板函数的未定义引用
- TensorRT (C++ API) 对"createNvOnnxParser_INTERNAL"的未定义引用
- 2个模板化类的非模板友元函数未定义引用错误
- 编译 libfluid 样本控制器时对"event_base_del_virtual"的未定义引用
- 获取对function_name的未定义引用
- 对 'std::thread::_M_start_thread CMake 的未定义引用进行基准测试
- 对结构方法的未定义引用
- 使用内联函数 c++ 的未定义引用
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 对 DLOPEN 的未定义引用
- QT C++中对全局变量的未定义引用
- 快速数学导致对"__pow_finite"的未定义引用
- 对 boost::system::d etail::system_category_instance 的未定义引用,从
- OpenCV 3.4.3 中对 'cv::String::d eallocate()' 错误的未定义引用