passing arrays / updating object arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: passing arrays / updating object arrays

 
1
  #31
Sep 6th, 2007
  1. toppings[z] = a[z] ;
If a is declared as a plain int, why are you subscripting it like an array of int? I think you should make the whole thing an array and that'll fix your problem.
  1. void Pizza::set ( int x , int y , int a[] , int i );
  1. void Pizza::set ( int x , int y , int a[] , int i )
  2. {
  3. type = x ;
  4. size = y ;
  5. for ( int z = 0 ; z < i ; z++ )
  6. toppings[z] = a[z] ;
  7. }
  1. pizza.set ( x , y , a, i ) ;
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: passing arrays / updating object arrays

 
0
  #32
Sep 6th, 2007
awesome!! It's working now!
Now I just need to do some slight modifications and add the computePrice function and I'm done.

thanks everyone!!
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.

-Edsger Dijkstra
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,358
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: passing arrays / updating object arrays

 
0
  #33
Sep 6th, 2007
I thought you had declared the third parameter as a reference to an array?
  1. void Pizza::set ( int x , int y , int (&a)[7] , int i )
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: passing arrays / updating object arrays

 
0
  #34
Sep 6th, 2007
i did but then i found out that c++ passes all arrays by reference by default.
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.

-Edsger Dijkstra
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: passing arrays / updating object arrays

 
0
  #35
Sep 6th, 2007
i did but then i found out that c++ passes all arrays by reference by default.
You have to be careful here. 'By reference' could mean two things, and only one of them is right. Thef irst by reference is an actual reference in C++ and arrays are not passed by that reference by default. To pass an array by actual reference in C++ you do it like Dave said.
  1. void Pizza::set ( int x , int y , int (&a)[7] , int i )
The second by reference is a fake reference that you get by passing a pointer by value and being able to get to the thing that the pointer points to. That's the default for arrays and that's why all of these are the same prototype.
  1. void Pizza::set ( int x , int y , int a[7] , int i )
  2. void Pizza::set ( int x , int y , int a[] , int i )
  3. void Pizza::set ( int x , int y , int *a , int i )
Woohoo! Finally something I know something about.
Last edited by Hamrick; Sep 6th, 2007 at 10:20 pm.
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: passing arrays / updating object arrays

 
0
  #36
Sep 7th, 2007
ah ok. so did i create a pointer?
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.

-Edsger Dijkstra
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,358
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: passing arrays / updating object arrays

 
0
  #37
Sep 7th, 2007
The answer depends on the current code.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: passing arrays / updating object arrays

 
0
  #38
Sep 7th, 2007
it's the same as post #31.
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.

-Edsger Dijkstra
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,358
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: passing arrays / updating object arrays

 
0
  #39
Sep 7th, 2007
That's a pointer (see #35).
http://c-faq.com/aryptr/aryptrparam.html
Last edited by Dave Sinkula; Sep 7th, 2007 at 12:33 am.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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