Pointers, Arrays and Creating Objs...

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

Pointers, Arrays and Creating Objs...

 
0
  #1
Nov 7th, 2008
Btw below is the key parts of my code - not all as there are loads of lines:

  1. //main.cpp
  2. #include <iostream>
  3. #include "Customer.h"
  4.  
  5. using namespace std;
  6.  
  7. void displayMainMenu();
  8. int enterMainChoice();
  9. void displayAccountMenu();
  10. int enterAccountChoice();
  11. int enterCustomerID();
  12. Customer createCustomer();
  13.  
  14. void main(void)
  15. {
  16. unsigned short numberOfCustomers = 0;
  17. Customer *Customers[10];
  18.  
  19. *Customers[numberOfCustomers] = createCustomer();
  20. numberOfCustomers++;
  21. }
  22.  
  23. Customer createCustomer()
  24. {
  25. string name;
  26. int age;
  27. cout << "\nPlease enter you name: ";
  28. cin >> name;
  29. cout << "Please enter you age: ";
  30. cin >> age;
  31. return Customer(name, age);
  32. }

----------------
Below is just the main function from Customer.cpp
----------------

  1. Customer::Customer(string name, int age)
  2. {
  3. this->name = name;
  4. this->age = age;
  5. }

Now it almost works. I can run it and it asks me for the information, but when it tries to add the actual customer is crashes in "xstring" with this error:

Unhandled exception at 0x643924f0 (msvcp90d.dll) in bankusingarrays.exe: 0xC0000005: Access violation reading location 0xcccccce8.

Now - I thought it would be because I am passing back something which can't be then put into the pointer array.

But I am not sure how to go about it - trying to create the object and store the address in the pointer array. I am pretty new with C++ so try and bare with me lol..

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 81
Reputation: minas1 is an unknown quantity at this point 
Solved Threads: 8
minas1's Avatar
minas1 minas1 is offline Offline
Junior Poster in Training

Re: Pointers, Arrays and Creating Objs...

 
0
  #2
Nov 7th, 2008
  1. Customer *Customers[10];
You create an array of pointers to that object.

Why don't you do:

  1.  
  2. Customer Customers[10];
  3. Customer[numberOfCustomers] = createCustomer();

-------------

1.Basically you are misuing pointers. In C and C++, when a variable is uninitialized, it contains a garbage value. Thus, Customers from 0 to 9 point to garbage values. They are pointers, which means they point in unknown places in the memory.

Then, you are assigning a value to that unknown memory location. This is the error. Before assigning, you have to be sure that the pointer points to a valid memory location of that object.
This is what you are doing.
  1. int main()
  2. {
  3. int *p, x;
  4.  
  5. x = 10;
  6. *p = x;
  7. }

This is what you should be doing.
  1. int main()
  2. {
  3. int *p, x;
  4.  
  5. x = 10;
  6. p = &x;
  7. }

If you still want an array of pointers to Customer objects, you should write:
  1. Customers[numberOfCustomers] = &(createCustomer()); //the address of the object

2.
Don't use void main(void). Just int main(). The use of void in the parentheses is redundant. It is required in C but not in C++.
Last edited by minas1; Nov 7th, 2008 at 1:49 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 114
Reputation: sidatra79 is an unknown quantity at this point 
Solved Threads: 8
sidatra79's Avatar
sidatra79 sidatra79 is offline Offline
Junior Poster

Re: Pointers, Arrays and Creating Objs...

 
0
  #3
Nov 7th, 2008
Hi
could u please post the Customer.h too?
"What we repeat defines what we are!!" Aristotle
-->
"looping.. defines Perfomance from O (n) ..up to O(n!)" Sidatra79 :D :D
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: Pointers, Arrays and Creating Objs...

 
0
  #4
Nov 7th, 2008
Originally Posted by sidatra79 View Post
Hi
could u please post the Customer.h too?
  1. #include <string>
  2. #pragma once
  3.  
  4. using namespace std;
  5. //----------------------
  6. class Customer
  7. {
  8.  
  9. private:
  10. string name;
  11. int age;
  12.  
  13. public:
  14.  
  15. //builder
  16. Customer(string name, int age);
  17.  
  18. //operations
  19. void Display();
  20. };

the display function doesn't have anything it btw, thats for later

Originally Posted by minas1
...
Ok thanks dude, briefly looking at it makes total sense, will look over my code to do as you say!
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 114
Reputation: sidatra79 is an unknown quantity at this point 
Solved Threads: 8
sidatra79's Avatar
sidatra79 sidatra79 is offline Offline
Junior Poster

Re: Pointers, Arrays and Creating Objs...

 
0
  #5
Nov 7th, 2008
Hi again,
have a look on this it might help u:

  1. #include <string>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. const int MAX_NO_OF_CUSTOMERS = 3;
  7.  
  8. class Customer
  9. {
  10. private:
  11. string name;
  12. int age;
  13. static unsigned short numberOfCustomers;
  14. public:
  15. Customer()
  16. {
  17. createCustomer();
  18. numberOfCustomers++;
  19. }
  20.  
  21. Customer(string name, int age)
  22. {
  23. this->name = name;
  24. this->age = age;
  25. }
  26.  
  27. Customer createCustomer()
  28. {
  29. string name;
  30. int age;
  31. cout << "\nPlease enter you name: ";
  32. cin >> name;
  33. cout << "Please enter you age: ";
  34. cin >> age;
  35. return Customer(name, age);
  36. }
  37. int getNumberofCustomers()
  38. {
  39. return numberOfCustomers;
  40. }
  41. };
  42. unsigned short Customer::numberOfCustomers = 0;
  43.  
  44.  
  45. int main()
  46. {
  47. int customersInTotal;
  48. Customer *Customers = new Customer[MAX_NO_OF_CUSTOMERS];
  49. customersInTotal=Customers->getNumberofCustomers();
  50. cout<<"Until now "<<customersInTotal<<" customers have been added."<<endl;
  51. return 0;
  52. }

i did on a hurry....maybe there are big mistakes
However it compiles and runs...

P.S: keep this in mind:
A no-argument constructor will be called automatically when dealing with an array of objects. But when u define some constructor with arguments, then u need to define a no-argument constructor too.
Last edited by sidatra79; Nov 7th, 2008 at 3:52 pm. Reason: added sth
"What we repeat defines what we are!!" Aristotle
-->
"looping.. defines Perfomance from O (n) ..up to O(n!)" Sidatra79 :D :D
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 81
Reputation: minas1 is an unknown quantity at this point 
Solved Threads: 8
minas1's Avatar
minas1 minas1 is offline Offline
Junior Poster in Training

Re: Pointers, Arrays and Creating Objs...

 
0
  #6
Nov 7th, 2008
Have you tried what I told you?
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