Main.cpp
This is where the problem lies.
#include <iostream>
#include <time.h>
#include <math.h>
#include <iomanip>
#include "LinkedList.h"
#include <fstream>
#include <string>
using namespace std;
void main(void)
{
LinkedList Customers;
Customer* newCustomer;
Entry* test;
int i = 0;
newCustomer = new Customer(1001, "Olly", "07/10/1988", "17 Bob Lane", "Hobbs Road", "UB3 RKL", "01234567891");
Customers.add(*newCustomer);
test = Customers.returnObject(2);
[B]i = test->info->returnCustomerID();[/B]
cout << i;
system("pause");
}
LinkedList.h
class Object
{
public:
Object(){;}
virtual ~Object(){;}
virtual ostream& printOn(ostream&) const = 0;
int returnCustomerID();
friend ostream& operator << (ostream& out, const Object& theObj)
{
theObj.printOn(out);
return out;
}
};
class Entry
{
public:
Entry* next;
Object* info;
Entry(Object* obj)
{
info = obj;
next = NULL;
}
~Entry(){delete info;}
};
class LinkedList
{
Entry* start_ptr;
int NoEntries;
public:
LinkedList();
~LinkedList();
Entry* returnStartPtr() const{return start_ptr;}
void add(Object& obj);
Entry* returnObject(int position);
ostream& print(ostream& co);
};
class Customer:public Object
{
int customerID;
public:
Customer(int customerID, string name, char dateOfBirth[], string address1, string address2, string postcode, string contactNumber)
{
this->customerID = customerID;
this->name = name;
strcpy(this->dateOfBirth, dateOfBirth);
this->address1 = address1;
this->address2 = address2;
this->postcode = postcode;
this->contactNumber = contactNumber;
}
int returnCustomerID()
{
return customerID;
}
ostream& printOn(ostream& co) const
{
co << "--------------------------" << endl;
co << "Customer ID: " << customerID << endl;
co << "Name: " << name << endl;
co << "Date of Birth: " << dateOfBirth << endl;
return co;
}
LinkedList.cpp
#include <iostream>
#include <stdlib.h>
#include <string>
#include "LinkedList.h"
using namespace std;
LinkedList::LinkedList()
{
start_ptr = NULL;
NoEntries = 0;
}
LinkedList::~LinkedList()
{
}
void LinkedList::add(Object& newEntry)
{
if(start_ptr == NULL)
{
Entry* newElement = new Entry(&newEntry);
start_ptr = newElement;
} else {
Entry* tempObj = start_ptr;
while(tempObj->next != NULL)
tempObj = tempObj->next;
Entry* newElement = new Entry(&newEntry);
tempObj->next = newElement;
}
NoEntries++;
}
Entry* LinkedList::returnObject(int position)
{
int count;
Entry *objectToReturn = start_ptr;
for(count = 0; count < position; count++)
objectToReturn = objectToReturn->next;
return objectToReturn;
}
- Ok here goes.
- Main.cpp is where the program is run. First of all I create a linkedlist of customers. This goes through Entry->Object->Customers. Don't ask why I have done it this way. I have to.
- I want to access variables inside Customer, outside, in Main.cpp.
- I have tried doing it as above, but the bolded line isn't liked. The program compiles but returns runtime error.
- Error 19 error LNK2019: unresolved external symbol "public: int __thiscall Object::returnCustomerID(void)" (?returnCustomerID@Object@@QAEHXZ) referenced in function _main main.obj bankusingarrays
- Ignore the name bankusingarrays - that was previous project, I know I am not using arrays lol.
I'm not really sure what to do now. As the program stands. I add 4 customers (I have only shown 1, named Olly, here but in actual program there are 4).
I want to enter the position in the linked list and return the object, to main, to then test the customerID against variable x. Obviously to do so I need to get the customerID from each object to then compare. This is what I am trying to do at the moment, get the ID of the Customer object in position 2, in linked list of Customers.
Ok. Well.... yes sorry its a long one, I couldn't think of how else to explain it!