我正在尝试将客户存储在链接列表中。我是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):错误C2440:'initializing':无法从'Customer *'转换为'int'
当我尝试将新数据条目添加到main.cpp上的链接列表时会发生这种情况。我总是在主菜单上的addtoList下面有一个红线。我认为这与客户有关,但我不确定。
Main.cpp
#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"));
}
list.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
};
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;
}
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);
// 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
和customer.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;
}
答案 0 :(得分:0)
void main ()
{
//construction
//LinkedList(); <-- This is not valid
LinkedList myList;
//add a new node to the last
// void addToList( new Customer("hhht","hhh","hhh","hhhkjk","klio")); <-- This
// is not valid
myList.addToList(new Customer("hhht","hhh","hhh","hhhkjk","klio"));
}
答案 1 :(得分:0)
好吧,为了让这些错误滚动,要声明LinkedList的实例:
LinkedList myList;
并调用该函数:
myList.addToList( new Customer("hhht","hhh","hhh","hhhkjk","klio"));
但是你似乎对编写自己的链表的c ++的最基本元素感到非常困惑 - 而且STL提供了一个。
答案 2 :(得分:0)
这些问题似乎相对容易解决:
void main ()
{
// This is wrong, it constructs an unnamed temporary.
//construction
// LinkedList();
// This is how you default construct a LinkedList item
LinkedList myList;
// This is wrong, you don't use the full signaure when calling a function
//add a new node to the last
// void addToList( new Customer("hhht","hhh","hhh","hhhkjk","klio"));
// This calls the addToList member function of the myList object
myList.addToList(new Customer(...));
}