我正在尝试将客户存储在链接列表中。我是 C++ 新手,所以我尝试将 int 链表调整为 Customer 链表。这是我的客户和列表代码以及它的主要运行。我得到的错误是:
1>c:\users\tashiemoto\desktop\c++\assignmentfinal\assignmentfinal\main4.cpp(12): 错误 C2182: 'addToList' : 非法使用类型 'void' 1>c:\users\tashiemoto\desktop\c++ \assignmentfinal\assignmentfinal\main4.cpp(12): error C2440: 'initializing' : cannot convert from 'Customer *' to 'int'
当我尝试将新数据条目添加到 main.cpp 上的链接列表时会发生这种情况。我在 main 的 addtoList 下总是有一条红线。我认为这与客户有关,但我不确定。
主文件
#include <iostream>
#include "customer.h"
#include "list.h"
void main ()
{
//construction
LinkedList();
//add a new node to the last
void addToList( new Customer("hhht","hhh","hhh","hhhkjk","klio"));
}
列表.h
#include "customer.h"
//forward declaration
class Node;
//class definition
class LinkedList
{
public:
//construction
LinkedList();
//add a new node to the last
void addToList(Customer *data);
//find an element in list and set current pointer
void find( int key);
//get data from element pointed at by current pointer
Customer* getCurrent(void);
//delete element pointed at by current pointer
void deleteCurrent(void);
private:
//data members
Node *_begin; //pointer to first element in list
Node *_end; //pointer to last element in list
Node *_current; //pointer to current element in list
};
列表.cpp
LinkedList::LinkedList()
{
//initialise node pointers
_begin = NULL;
_end = NULL;
_current = NULL;
}
void LinkedList::addToList(Customer *data)
{
Node *newNodePtr;
//craete new instance of Node
newNodePtr = new Node;
//set data
newNodePtr->setData(data);
//check if list is empty
if( _begin == NULL)
{
_begin = newNodePtr;
}
else
{
newNodePtr->setPrevNode(_end);
_end->setNextNode(newNodePtr);
}
//set current pointer end end pointer
_end = newNodePtr;
_current = newNodePtr;
}
客户.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);
// 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
和客户.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;
}
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;
}