0

I am trying to store a customer in a linked list. I am new to C++ so I have tried to adapt a int linked list to a Customer linked list. This is my code for the customer and list and the main running of it. The errors I get are:

1>c:\users\tashiemoto\desktop\c++\assignmentfinal\assignmentfinal\main4.cpp(12): error C2182: 'addToList' : illegal use of type 'void' 1>c:\users\tashiemoto\desktop\c++\assignmentfinal\assignmentfinal\main4.cpp(12): error C2440: 'initializing' : cannot convert from 'Customer *' to 'int'

Which happens when I try and add a new data entry to the linked list on main.cpp. I always have a red line under addtoList on main. I think it has something to do with customer but I'm not sure.

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

and 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;
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Learning
  • 15
  • 1
  • 5
  • 3
    Nobody has time to go through this wall of code.!! Please post only the pertinent code. – Coding Mash Nov 28 '12 at 15:57
  • You should get a good book http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Caribou Nov 28 '12 at 16:08
  • in fact... what happened to http://stackoverflow.com/questions/13597327/c-linked-list-with-objects-that-is-coming-up-with-the-lnk2019-error – Caribou Nov 28 '12 at 16:12
  • I didn't make a back-up copy of the work and I changed it so much It completely broke, so I stripped it back to the basics and restarted. This one seems clearer. And I know I should have a good book, of the ones I've rented out not a single thing on linked lists and the lecture notes are all theory no examples. So i'm a little tired and am now making stupid errors since for some reason without a linked list you have no chance of getting over 60%. – Learning Nov 28 '12 at 16:28

3 Answers3

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

}
dweeves
  • 5,525
  • 22
  • 28
0

Well, to get ya rolling on those errors, to declare an instance of the LinkedList:

LinkedList myList;

And to call the function:

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

But you seem to be very confused about the most basic elements of c++ to be writing your own linked list - and the STL provides one anyway.

Grimm The Opiner
  • 1,778
  • 11
  • 29
0

The problems appear to be relatively easy to address:

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

}
Chad
  • 18,706
  • 4
  • 46
  • 63