| | |
Inheritence, .h files and its a long one
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 29
Reputation:
Solved Threads: 1
Main.cpp
This is where the problem lies.
LinkedList.h
LinkedList.cpp
- 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!
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);
i = test->info->returnCustomerID();
cout << i;
system("pause");
}LinkedList.h
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
#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!
Last edited by Lokolo; Nov 19th, 2008 at 12:10 pm.
C++ Syntax (Toggle Plain Text)
i = test->info->returnCustomerID();
Did you ever declare 'i' ?
I would love to change the world, but they won't give me the source code
•
•
Join Date: Nov 2008
Posts: 29
Reputation:
Solved Threads: 1
•
•
•
•
C++ Syntax (Toggle Plain Text)
i = test->info->returnCustomerID();
Did you ever declare 'i' ?
int i = 0; <---- inside void main(void)
•
•
Join Date: Nov 2008
Posts: 29
Reputation:
Solved Threads: 1
•
•
•
•
>unresolved external symbol "public: int __thiscall Object::returnCustomerID(void)"
returnCustomerID is declared in Object (why?), not defined, and it's not virtual. I'm not surprised your linker is complaining that it can't find the definition.
Because Customers are a type of Object.
And not sure about define, don't understand?
Last edited by Lokolo; Nov 19th, 2008 at 12:15 pm.
>And not sure about define, don't understand?
Functions need a body. No body, and the linker bitches at you. Example:
n'est-ce pas?
Functions need a body. No body, and the linker bitches at you. Example:
C++ Syntax (Toggle Plain Text)
void foo(); int main() { foo(); // Linker error, foo has no body }
I'm here to prove you wrong.
•
•
Join Date: Nov 2008
Posts: 29
Reputation:
Solved Threads: 1
•
•
•
•
>And not sure about define, don't understand?
Functions need a body. No body, and the linker bitches at you. Example:
n'est-ce pas?C++ Syntax (Toggle Plain Text)
void foo(); int main() { foo(); // Linker error, foo has no body }
i.e. I declare it in Object, write the body in Customer, and it would link the 2 together as Customers are Objects (as declared in main.cpp)
By all means if I declare the body in object i.e. return CustomerID, inside Object it won't like it because there is no CustomerID, because thats in Customer, not Object.
Last edited by Lokolo; Nov 19th, 2008 at 12:21 pm.
>i.e. I declare it in Object, write the body in Customer, and it would link
>the 2 together as Customers are Objects (as declared in main.cpp)
To translate: You're looking for the behavior of a pure virtual member function without declaring the member function as pure virtual. How silly.
Here's the magic incantation to make your dreams reality:
>the 2 together as Customers are Objects (as declared in main.cpp)
To translate: You're looking for the behavior of a pure virtual member function without declaring the member function as pure virtual. How silly.
Here's the magic incantation to make your dreams reality:
C++ Syntax (Toggle Plain Text)
virtual int returnCustomerID() = 0;
I'm here to prove you wrong.
•
•
Join Date: Nov 2008
Posts: 29
Reputation:
Solved Threads: 1
•
•
•
•
>i.e. I declare it in Object, write the body in Customer, and it would link
>the 2 together as Customers are Objects (as declared in main.cpp)
To translate: You're looking for the behavior of a pure virtual member function without declaring the member function as pure virtual. How silly.
Here's the magic incantation to make your dreams reality:
C++ Syntax (Toggle Plain Text)
virtual int returnCustomerID() = 0;
That gets past that error but creats more errors in the rest of my program. I have more than 1 type of Object, one is Accounts which is abstract, I have types of Account e.g.
C++ Syntax (Toggle Plain Text)
lass CurrentAccount:virtual public Account { private: public: CurrentAccount(int customerID, int accountID, int balance):Account(customerID, accountID, balance, savingsRate, borrowRate, overdraftLimit, "Current Account") { savingsRate = 1; borrowRate = 12; overdraftLimit = 500; } };
(Account is the same as Customer but with different variables, also is public Object).
Its not liking this as my Current Account is abstract.
Error 8 error C2259: 'CurrentAccount' : cannot instantiate abstract class d:\c++\bankusingarrays\main.cpp 130 bankusingarrays
This is when I try to create a newAccount (exactly the same as above with newCustomer)
i.e.
C++ Syntax (Toggle Plain Text)
newAccount = new CurrentAccount(1002, 100002, 35);
So basically it doesn't like me creating new types of Account.
>So does making it = 0 make it pure virtual?
Sadly, yes. It's terribly obscure and clever on the part of C++'s designers (much like destructors being the constructor name with a leading tilde), which is a shame.
>So basically it doesn't like me creating new types of Account.
Classes that derive from Account must override the pure virtual member functions in the derived class. If you don't, the derived class inherits the abstract status. You say Account is abstract, so it should have at least one such member function, yet your CurrentAccount class only defines a constructor.
Sadly, yes. It's terribly obscure and clever on the part of C++'s designers (much like destructors being the constructor name with a leading tilde), which is a shame.
>So basically it doesn't like me creating new types of Account.
Classes that derive from Account must override the pure virtual member functions in the derived class. If you don't, the derived class inherits the abstract status. You say Account is abstract, so it should have at least one such member function, yet your CurrentAccount class only defines a constructor.
I'm here to prove you wrong.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Recursive program issue
- Next Thread: .h source file acting up
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






