| | |
Pointers, Arrays and Creating Objs...
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 29
Reputation:
Solved Threads: 1
Btw below is the key parts of my code - not all as there are loads of lines:
----------------
Below is just the main function from Customer.cpp
----------------
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!
C++ Syntax (Toggle Plain Text)
//main.cpp #include <iostream> #include "Customer.h" using namespace std; void displayMainMenu(); int enterMainChoice(); void displayAccountMenu(); int enterAccountChoice(); int enterCustomerID(); Customer createCustomer(); void main(void) { unsigned short numberOfCustomers = 0; Customer *Customers[10]; *Customers[numberOfCustomers] = createCustomer(); numberOfCustomers++; } Customer createCustomer() { string name; int age; cout << "\nPlease enter you name: "; cin >> name; cout << "Please enter you age: "; cin >> age; return Customer(name, age); }
----------------
Below is just the main function from Customer.cpp
----------------
C++ Syntax (Toggle Plain Text)
Customer::Customer(string name, int age) { this->name = name; this->age = age; }
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!
C++ Syntax (Toggle Plain Text)
Customer *Customers[10];
Why don't you do:
C++ Syntax (Toggle Plain Text)
Customer Customers[10]; 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)
int main() { int *p, x; x = 10; *p = x; }
This is what you should be doing.
C++ Syntax (Toggle Plain Text)
int main() { int *p, x; x = 10; p = &x; }
If you still want an array of pointers to Customer objects, you should write:
C++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Nov 2008
Posts: 29
Reputation:
Solved Threads: 1
C++ Syntax (Toggle Plain Text)
#include <string> #pragma once using namespace std; //---------------------- class Customer { private: string name; int age; public: //builder Customer(string name, int age); //operations void Display(); };
the display function doesn't have anything it btw, thats for later
•
•
•
•
Originally Posted by minas1
...
Hi again,
have a look on this it might help u:
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.
have a look on this it might help u:
c++ Syntax (Toggle Plain Text)
#include <string> #include <iostream> using namespace std; const int MAX_NO_OF_CUSTOMERS = 3; class Customer { private: string name; int age; static unsigned short numberOfCustomers; public: Customer() { createCustomer(); numberOfCustomers++; } Customer(string name, int age) { this->name = name; this->age = age; } Customer createCustomer() { string name; int age; cout << "\nPlease enter you name: "; cin >> name; cout << "Please enter you age: "; cin >> age; return Customer(name, age); } int getNumberofCustomers() { return numberOfCustomers; } }; unsigned short Customer::numberOfCustomers = 0; int main() { int customersInTotal; Customer *Customers = new Customer[MAX_NO_OF_CUSTOMERS]; customersInTotal=Customers->getNumberofCustomers(); cout<<"Until now "<<customersInTotal<<" customers have been added."<<endl; return 0; }
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
-->
"looping.. defines Perfomance from O (n) ..up to O(n!)" Sidatra79 :D :D
![]() |
Other Threads in the C++ Forum
- Previous Thread: hellow
- Next Thread: Need help...Easy fix but I can't figure it out.
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





