943,856 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 648
  • C++ RSS
Nov 7th, 2008
0

Pointers, Arrays and Creating Objs...

Expand Post »
Btw below is the key parts of my code - not all as there are loads of lines:

C++ Syntax (Toggle Plain Text)
  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
----------------

C++ Syntax (Toggle Plain Text)
  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!
Reputation Points: 11
Solved Threads: 1
Light Poster
Lokolo is offline Offline
29 posts
since Nov 2008
Nov 7th, 2008
0

Re: Pointers, Arrays and Creating Objs...

C++ Syntax (Toggle Plain Text)
  1. Customer *Customers[10];
You create an array of pointers to that object.

Why don't you do:

C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. int *p, x;
  4.  
  5. x = 10;
  6. *p = x;
  7. }

This is what you should be doing.
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 13
Solved Threads: 8
Junior Poster in Training
minas1 is offline Offline
81 posts
since Nov 2008
Nov 7th, 2008
0

Re: Pointers, Arrays and Creating Objs...

Hi
could u please post the Customer.h too?
Reputation Points: 46
Solved Threads: 8
Junior Poster
sidatra79 is offline Offline
114 posts
since Feb 2008
Nov 7th, 2008
0

Re: Pointers, Arrays and Creating Objs...

Click to Expand / Collapse  Quote originally posted by sidatra79 ...
Hi
could u please post the Customer.h too?
C++ Syntax (Toggle Plain Text)
  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

Quote originally posted by minas1 ...
...
Ok thanks dude, briefly looking at it makes total sense, will look over my code to do as you say!
Reputation Points: 11
Solved Threads: 1
Light Poster
Lokolo is offline Offline
29 posts
since Nov 2008
Nov 7th, 2008
0

Re: Pointers, Arrays and Creating Objs...

Hi again,
have a look on this it might help u:

c++ Syntax (Toggle Plain Text)
  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
Reputation Points: 46
Solved Threads: 8
Junior Poster
sidatra79 is offline Offline
114 posts
since Feb 2008
Nov 7th, 2008
0

Re: Pointers, Arrays and Creating Objs...

Have you tried what I told you?
Reputation Points: 13
Solved Threads: 8
Junior Poster in Training
minas1 is offline Offline
81 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: hellow
Next Thread in C++ Forum Timeline: Need help...Easy fix but I can't figure it out.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC