【问题标题】:Linked list with objects not inserting data带有未插入数据的对象的链表
【发布时间】:2012-11-28 15:57:09
【问题描述】:

我正在尝试将客户存储在链接列表中。我是 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 下总是有一条红线。我认为这与客户有关,但我不确定。

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;
}

客户.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;
}

【问题讨论】:

  • 没有人有时间穿过这堵代码墙。!!请只发布相关代码。
  • 你应该得到一本好书stackoverflow.com/questions/388242/…
  • 事实上……stackoverflow.com/questions/13597327/… 发生了什么事
  • 我没有制作作品的备份副本,我更改了很多它完全崩溃了,所以我将它剥离回基础并重新启动。这个好像比较清楚。而且我知道我应该有一本好书,那些我租过的书没有链接列表上的任何东西,而且讲义都是理论没有例子。所以我有点累了,现在犯了愚蠢的错误,因为由于某种原因没有链表,你没有机会超过 60%。

标签: c++ object visual-c++ linked-list


【解决方案1】:

这些问题似乎比较容易解决:

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(...));

}

【讨论】:

    【解决方案2】:
    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"));
    
    }
    

    【讨论】:

      【解决方案3】:

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

      LinkedList myList;
      

      并调用函数:

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

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

      【讨论】:

        猜你喜欢
        • 2021-10-31
        • 2022-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多