C++ 带有出现LNK2019错误的对象的链接列表
C++ Linked list with Objects that is coming up with the LNK2019 error
基本上,这是我C++的作业项目,我对C++很陌生,我遇到了这个错误。main == 中的注释代码也有错误,这不是匹配的操作数
1>Main2.obj : 错误 LNK2019: 未解析的外部符号 "class std::basic_ostream> & __cdecl 运算符<<(class std::basic_ostream> &,class Customer &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAVCustomer@@@Z) 在函数"void __cdecl display(struct Node *)" (?display@@YAXPAUNode@@@Z) 中引用
这是代码主.cpp
#include <iostream>
#include "account.h"
#include "cheque.h"
#include "customer.h"
using namespace std;
struct Node {
Node* m_next;
Customer& m_customer;
Node(Customer& customer) : m_next(0), m_customer(customer) {}
};
// only for the 1st Node
void initNode(struct Node *head,Customer n){
head->m_customer = n;
head->m_next =NULL;
}
// apending
void addNode(struct Node *head, Customer n) {
Node *newNode = new Node(n);
newNode->m_customer = n;
newNode->m_next = NULL;
Node *cur = head;
while(cur) {
if(cur->m_next == NULL) {
cur->m_next = newNode;
return;
}
cur = cur->m_next;
}
}
void insertFront(struct Node **head, Customer n) {
Node *newNode = new Node(n);
newNode->m_customer = n;
newNode->m_next = *head;
*head = newNode;
}
//struct Node *searchNode(struct Node *head, Customer n) {
//Node *cur = head;
//while(cur) {
//if(cur->m_customer == n) return cur;
//cur = cur->m_next;
//}
//cout << "No Node " << n << " in list.n";
//}
bool deleteNode(struct Node **head, Node *ptrDel) {
Node *cur = *head;
if(ptrDel == *head) {
*head = cur->m_next;
delete ptrDel;
return true;
}
while(cur) {
if(cur->m_next == ptrDel) {
cur->m_next = ptrDel->m_next;
delete ptrDel;
return true;
}
cur = cur->m_next;
}
return false;
}
void deleteLinkedList(struct Node **node)
{
struct Node *tmpNode;
while(*node) {
tmpNode = *node;
*node = tmpNode->m_next;
delete tmpNode;
}
}
void display(struct Node *head) {
Node *list = head;
while(list) {
cout << list->m_customer << " ";
list = list->m_next;
}
cout << endl;
cout << endl;
}
int main()
{
Customer test1("sdfs","sdf2","dsfpppsf","fdgdfg","fdgdsffg");
struct Node *newHead;
struct Node *head = new Node(test1);
Customer test2("sdsffs","sdfhhmj2","dsfhfsf","fdgdfgs","fdsggdfg");
Customer test3("sdsdfs","sdllllf2","dsfldfgsf","fdgaghdfg","fdgbvcbdfg");
Customer test4("sdgnfgfs","ssdfsdf2","dsfhjhdsf","fdbvcgdfg","fdgsfddfg");
addNode(head,test2);
display(head);
addNode(head,test3);
display(head);
insertFront(&head,test4);
display(head);
cout << "Deleting the copied listn";
deleteLinkedList(&newHead);
display(newHead);
return 0;
}
客户.cpp
#include "customer.h"
using namespace std;
Customer::Customer(string init_name, string init_address, string init_telephone, string init_sex, string init_dob)
{
name = init_name;
address = init_address;
telephone = init_telephone;
sex = init_sex;
dob = init_dob;
}
void Customer::showPersonDetails(void)
{
cout << "Name : "
<< name << endl;
cout << "Address : "
<< address << endl ;
cout << "Telephone : "
<< telephone << endl;
cout << "Sex : "
<< sex << endl ;
cout << "Date of Birth : "
<< dob << endl;
}
最后是客户。
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include <iostream>
#include<string>
using namespace std;
class Customer
{
private:
//
// class members
//
string name;
string address;
string telephone;
string sex;
string dob;
public:
// Constructor
Customer(string init_name, string init_address, string init_telephone, string init_sex, string init_dob);
// a print method
void showPersonDetails(void);
void changeDetails(void);
// This operator is a friend of the class, but is NOT a member of the // class:
friend ostream& operator <<(ostream& s, Customer& a);
};
// This is the prototype for the overload
ostream& operator <<(ostream& s, Customer& a);
#endif
在标头中,您有以下声明:
// This operator is a friend of the class, but is NOT a member of the class:
friend ostream& operator <<(ostream& s, Customer& a);
您的 cpp 文件中没有定义 - 您需要提供定义。
ostream& operator <<(ostream& s, Customer& a)
{
s << a.name << ... etc...
return s;
}
出现链接器错误的原因是声明存在,但定义不存在。
在Main.cpp
中,您有使用 operator<<
进行Customer
的 display
函数:
void display(struct Node *head) {
Node *list = head;
while(list) {
cout << list->m_customer << " "; // this line here uses operator<< for Customer
list = list->m_next;
}
它编译是因为operator<<
存在于标头中,因此可以找到符号...但是在链接阶段找不到对象定义(operator<<
的主体),因此您会收到unresolved external
链接器错误。
您应该在客户中实现operator<<
客户类.cpp
friend ostream& operator <<(ostream& s, Customer& a)
{
s << "Name : " << a.name << endl;
s << "Address : " << a.address << endl ;
s << "Telephone : " << a.telephone << endl;
s << "Sex : " << a.sex << endl ;
s << "Date of Birth : "<< a.dob << endl;
return s;
}
相关文章:
- CMake-按正确顺序将项目与C运行时对象文件链接
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- 返回的指向C++对象的链接是什么样的
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 返回一个带有 std::move 的对象并链接函数
- 链接列表在 cpp 中包含不同的对象类
- 链接时,不同静态库中的同一对象文件
- 通过 Gazebo 世界插件将静态对象附加到机器人链接
- 共享对象、符号、C/C++ 库链接和加载
- 尝试使用 extern "C" 调用 C 中的C++方法,得到"undefined reference to"对象的链接器错误
- 仅为从某种语言编译的对象添加链接库?
- C++ 为什么要级联共享对象链接
- 从最基本的Qt应用程序重新链接对象 - 链接器错误
- Natty Narwhal和Oneiric Ocelot之间共享对象链接的差异
- 静态成员对象链接错误
- 与模板函数中的局部派生对象链接错误
- 使用Cuda对象链接与Cmake
- Qt元对象链接器问题
- 使用MVP,如何从与同一模型对象链接的另一个视图创建视图
- 创建一个简单的"hello world"并将其与 2 个预编译对象链接在一起