0

我正在尝试将客户存储在链接列表中。我是 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;
}
4

3 回答 3

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"));

}
于 2012-11-28T16:03:00.553 回答
0

好吧,为了让你解决这些错误,声明一个 LinkedList 的实例:

LinkedList myList;

并调用该函数:

myList.addToList( new Customer("hhht","hhh","hhh","hhhkjk","klio"));

但是您似乎对编写自己的链表的 c++ 最基本元素感到非常困惑——而 STL 无论如何都提供了一个。

于 2012-11-28T16:03:33.153 回答
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(...));

}
于 2012-11-28T16:04:11.500 回答