vector trouble

Reply

Join Date: Nov 2008
Posts: 4
Reputation: arreyes is an unknown quantity at this point 
Solved Threads: 0
arreyes arreyes is offline Offline
Newbie Poster

vector trouble

 
0
  #1
Nov 18th, 2008
If you make a vector of a class of no designated size, as you reserve spaces inside the vector will it just make a default instance from the constructor?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: vector trouble

 
0
  #2
Nov 18th, 2008
Probably, you can check it out pretty easy.

Be careful of the word size. size is frequently used to indicate the actual number of elements currently in the vector as opposed to the number of elements for which space is allocated for vector use, which might be called capacity or size.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: vector trouble

 
0
  #3
Nov 18th, 2008
Fortunately you CAN'T invent a "class of no designated size" in C++. For every class its objects have fixed and defined size (remember operator sizeof).
If you mean a class with dynamically allocated members then it's a problem of this class constructor, not a vector container. That's why class constructors were invented.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 4
Reputation: arreyes is an unknown quantity at this point 
Solved Threads: 0
arreyes arreyes is offline Offline
Newbie Poster

Re: vector trouble

 
0
  #4
Nov 18th, 2008
i meant i created a vector of unspecified size. but when I reserve a spot then try to alter the instance an error pops up and I get a window telling me that the program has to close because it has run into an error. I just don't know what is going on.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 922
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: vector trouble

 
0
  #5
Nov 18th, 2008
Post code.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 51
Reputation: Manutebecker is an unknown quantity at this point 
Solved Threads: 2
Manutebecker's Avatar
Manutebecker Manutebecker is offline Offline
Junior Poster in Training

Re: vector trouble

 
0
  #6
Nov 18th, 2008
Your question is difficult to understand, but if you mean will it construct what you have in a default constructor to each instance of said class, then yes it should.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: vector trouble

 
0
  #7
Nov 19th, 2008
If you are using std::vector class then probable causes are:
1. You reserve vector size then try to access its elements, but reserve member function does not construct vector elements. You need resize (not reserve) vector if you want to access new elements immediatly.
2. Your class has bad default constructor (constructor without arguments). For example:
  1. class Bad {
  2. Bad() {} // Forgot to initialize pDynArr with 0.
  3. Bad(const char* p, int n) {
  4. pDynArr = new char[n];
  5. }
  6. ~Bad() { delete [] pDynArr;
  7. private:
  8. char* pDynArr;
  9. };
  10. ...
  11. std::vector<Bad> troubles;
  12. ...
  13. troubles.resize(10);
  14. ...
New Bad objects created after vector<Bad>::resize(k) are true bad. There are garbage values of pDynArr member in all 10 vector elements. The destructor of troubles calls Bad destructor for every 10 elements, the last one call delete [] pDynArr for undefined pointer.

Follow MosaicFuneral's advice ...
Last edited by ArkM; Nov 19th, 2008 at 2:13 am.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC