Inheritence, .h files and its a long one

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 29
Reputation: Lokolo is an unknown quantity at this point 
Solved Threads: 1
Lokolo Lokolo is offline Offline
Light Poster

Inheritence, .h files and its a long one

 
0
  #1
Nov 19th, 2008
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);
	i = test->info->returnCustomerID();
	cout << i;
	system("pause");
}

LinkedList.h

  1. class Object
  2. {
  3. public:
  4. Object(){;}
  5. virtual ~Object(){;}
  6. virtual ostream& printOn(ostream&) const = 0;
  7. int returnCustomerID();
  8. friend ostream& operator << (ostream& out, const Object& theObj)
  9. {
  10. theObj.printOn(out);
  11. return out;
  12. }
  13. };
  14.  
  15. class Entry
  16. {
  17. public:
  18. Entry* next;
  19. Object* info;
  20. Entry(Object* obj)
  21. {
  22. info = obj;
  23. next = NULL;
  24. }
  25. ~Entry(){delete info;}
  26. };
  27.  
  28. class LinkedList
  29. {
  30. Entry* start_ptr;
  31. int NoEntries;
  32. public:
  33. LinkedList();
  34. ~LinkedList();
  35. Entry* returnStartPtr() const{return start_ptr;}
  36. void add(Object& obj);
  37. Entry* returnObject(int position);
  38. ostream& print(ostream& co);
  39. };
  40.  
  41. class Customer:public Object
  42. {
  43. int customerID;
  44.  
  45. public:
  46. Customer(int customerID, string name, char dateOfBirth[], string address1, string address2, string postcode, string contactNumber)
  47. {
  48. this->customerID = customerID;
  49. this->name = name;
  50. strcpy(this->dateOfBirth, dateOfBirth);
  51. this->address1 = address1;
  52. this->address2 = address2;
  53. this->postcode = postcode;
  54. this->contactNumber = contactNumber;
  55. }
  56.  
  57. int returnCustomerID()
  58. {
  59. return customerID;
  60. }
  61.  
  62. ostream& printOn(ostream& co) const
  63. {
  64. co << "--------------------------" << endl;
  65. co << "Customer ID: " << customerID << endl;
  66. co << "Name: " << name << endl;
  67. co << "Date of Birth: " << dateOfBirth << endl;
  68. return co;
  69. }

LinkedList.cpp

  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <string>
  4. #include "LinkedList.h"
  5.  
  6. using namespace std;
  7.  
  8. LinkedList::LinkedList()
  9. {
  10. start_ptr = NULL;
  11. NoEntries = 0;
  12. }
  13.  
  14. LinkedList::~LinkedList()
  15. {
  16. }
  17.  
  18. void LinkedList::add(Object& newEntry)
  19. {
  20. if(start_ptr == NULL)
  21. {
  22. Entry* newElement = new Entry(&newEntry);
  23. start_ptr = newElement;
  24. } else {
  25. Entry* tempObj = start_ptr;
  26. while(tempObj->next != NULL)
  27. tempObj = tempObj->next;
  28. Entry* newElement = new Entry(&newEntry);
  29. tempObj->next = newElement;
  30. }
  31. NoEntries++;
  32. }
  33.  
  34. Entry* LinkedList::returnObject(int position)
  35. {
  36. int count;
  37. Entry *objectToReturn = start_ptr;
  38. for(count = 0; count < position; count++)
  39. objectToReturn = objectToReturn->next;
  40. return objectToReturn;
  41. }

- 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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 118
Reputation: chococrack is on a distinguished road 
Solved Threads: 14
chococrack's Avatar
chococrack chococrack is offline Offline
Junior Poster

Re: Inheritence, .h files and its a long one

 
0
  #2
Nov 19th, 2008
  1. 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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,606
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Inheritence, .h files and its a long one

 
0
  #3
Nov 19th, 2008
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 29
Reputation: Lokolo is an unknown quantity at this point 
Solved Threads: 1
Lokolo Lokolo is offline Offline
Light Poster

Re: Inheritence, .h files and its a long one

 
0
  #4
Nov 19th, 2008
Originally Posted by chococrack View Post
  1. i = test->info->returnCustomerID();

Did you ever declare 'i' ?
lol yes I am not that silly. My codes a lot bigger than that, I just didn't want to waste too much space.

int i = 0; <---- inside void main(void)
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 29
Reputation: Lokolo is an unknown quantity at this point 
Solved Threads: 1
Lokolo Lokolo is offline Offline
Light Poster

Re: Inheritence, .h files and its a long one

 
0
  #5
Nov 19th, 2008
Originally Posted by Narue View Post
>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.
Virtual doesn't do anything. (gives the same error but with virtual infront of the function error)

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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,606
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Inheritence, .h files and its a long one

 
0
  #6
Nov 19th, 2008
>And not sure about define, don't understand?
Functions need a body. No body, and the linker bitches at you. Example:
  1. void foo();
  2.  
  3. int main()
  4. {
  5. foo(); // Linker error, foo has no body
  6. }
n'est-ce pas?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 29
Reputation: Lokolo is an unknown quantity at this point 
Solved Threads: 1
Lokolo Lokolo is offline Offline
Light Poster

Re: Inheritence, .h files and its a long one

 
0
  #7
Nov 19th, 2008
Originally Posted by Narue View Post
>And not sure about define, don't understand?
Functions need a body. No body, and the linker bitches at you. Example:
  1. void foo();
  2.  
  3. int main()
  4. {
  5. foo(); // Linker error, foo has no body
  6. }
n'est-ce pas?
Ok I get that but I have returnCustomerID in Customer class. Is that not enough? I thought it would pick it up

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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,606
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Inheritence, .h files and its a long one

 
0
  #8
Nov 19th, 2008
>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:
  1. virtual int returnCustomerID() = 0;
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 29
Reputation: Lokolo is an unknown quantity at this point 
Solved Threads: 1
Lokolo Lokolo is offline Offline
Light Poster

Re: Inheritence, .h files and its a long one

 
0
  #9
Nov 19th, 2008
Originally Posted by Narue View Post
>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:
  1. virtual int returnCustomerID() = 0;
lol thanks. So does making it = 0 make it pure virtual?

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.

  1. lass CurrentAccount:virtual public Account
  2. {
  3. private:
  4.  
  5. public:
  6. CurrentAccount(int customerID, int accountID, int balance):Account(customerID, accountID, balance, savingsRate, borrowRate, overdraftLimit, "Current Account")
  7. {
  8. savingsRate = 1;
  9. borrowRate = 12;
  10. overdraftLimit = 500;
  11. }
  12. };

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

  1. newAccount = new CurrentAccount(1002, 100002, 35);

So basically it doesn't like me creating new types of Account.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,606
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Inheritence, .h files and its a long one

 
1
  #10
Nov 19th, 2008
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC