help with dynamic memory

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

Join Date: Sep 2004
Posts: 7,844
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 752
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: help with dynamic memory

 
0
  #11
May 14th, 2007
>you are trying to access a private member with your object in the main function.
No, he's not. He's trying to access a private member of an object from within a public member function of the same class. Note the function definition:
  1. void Names::enter_new()
Unless the rules have recently changed without my knowing, that's not a correct definition of main. The access is legal.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,844
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 752
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: help with dynamic memory

 
0
  #12
May 14th, 2007
Here's an example of what I mean, m3an, since words don't seem to have sufficient effect. I've corrected your errors and added enough to "use dynamic memory and classes together":
  1. #include <iostream>
  2. #include <string>
  3. #include <new>
  4.  
  5. using namespace std;
  6.  
  7. class Names {
  8. private:
  9. string title;
  10. string *p;
  11. int i;
  12. public:
  13. Names ( const string& init ): title ( init ) {}
  14. ~Names() { delete[] p; }
  15. void enter_new();
  16. void display();
  17. };
  18.  
  19. void Names::enter_new()
  20. {
  21. cout<<"How many movies would you like to type? ";
  22. cin>> i;
  23. cin.ignore();
  24.  
  25. p = new(nothrow) string[i];
  26.  
  27. if ( p == 0 )
  28. cout << "Error: memory could not be allocated";
  29. else {
  30. for ( int a = 0; a < i; a++ ) {
  31. cout<<"Actor "<< a + 1 <<": ";
  32. getline ( cin, p[a] );
  33. }
  34. }
  35. }
  36.  
  37. void Names::display()
  38. {
  39. cout<<"Title: "<< title <<"\nActors:\n";
  40.  
  41. for ( int a = 0; a < i; a++ )
  42. cout<<'\t'<< p[a] <<'\n';
  43. }
  44.  
  45. int main()
  46. {
  47. Names film ( "My film" );
  48.  
  49. film.enter_new();
  50. film.display();
  51. }
Have I still not answered your question?
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 21
Reputation: desijays is an unknown quantity at this point 
Solved Threads: 1
desijays desijays is offline Offline
Newbie Poster

Re: help with dynamic memory

 
0
  #13
May 14th, 2007
Originally Posted by m3an View Post
its part of a dvd program. i want all the objects created and manipulated by the program to be stored in a dynamic array that might grow during the execution of the program. I want to program to store the info in a dynamic array that i can call anywhere to list the info.im having trouble with using dynamic memory and classes together, ...please help, thanks


  1.  
  2. class Names
  3. {
  4. private:
  5. string title;
  6. int i;
  7. int * p;
  8.  
  9. public:
  10. void enter_new(); // sets names of actors to the DVD
  11. }film;
  12.  
  13.  
  14. void Names::enter_new()
  15. {char ans;
  16. cout << "How many movies would you like to type? ";
  17. cin >> film.i; //I was talking about this Narua
  18. int ii= film.i;
  19. (film.p)= new int[ii];
  20.  
  21. if ((film.p) == 0)
  22. cout << "Error: memory could not be allocated";
  23. else
  24. {
  25. for (int a=1; a<(ii); a++)
  26. {cout<<"\n Please enter the name: "<<endl;
  27. cin >> (film.p)[a];
  28. }
  29. delete[] p;
  30. }
Were you referring to something else mate?
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 11
Reputation: m3an is an unknown quantity at this point 
Solved Threads: 1
m3an m3an is offline Offline
Newbie Poster

Re: help with dynamic memory

 
0
  #14
May 14th, 2007
>I guess I'm not smart enough. Please point out where you said a dynamic array was required. The closest you got was saying "I want", which clearly says that you have a choice. Therefore, I suggested a better choice.
wel i did not blame you for that, i mearly pointed out the fact. since my original post was not clear enough for you.


>Then make it shorter for posting. There's a sticky at the top of this forum that tells you how to post like an intelligent human being.
i did make it short for posting. look at my post....


>Then you should say "thanks" and leave it because my comment was accurate when you posted. As I said, I'm not psychic, so I don't know what changes you made after you posted.
well i added nothrow bofore you suggested, so i was just letting u know. But thanks anyways.


>BS. Maybe you should take some lessons in reading for comprehension, because the fourth post in this thread answers your question fully. If it doesn't, you asked the wrong question.
thats funny. Read the first line in the 5th post and then see who should take lessons.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,844
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 752
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: help with dynamic memory

 
0
  #15
May 14th, 2007
>Read the first line in the 5th post and then see who should take lessons.
I still think it's you. You see, you had some weird looping problem, but my code worked perfectly. So, since I answered the question you asked, and your broken attempt at my answer didn't work, I didn't answer your question? That's some good logic, and it explains why you can't write working code. Please don't take your incompetence out on me.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 11
Reputation: m3an is an unknown quantity at this point 
Solved Threads: 1
m3an m3an is offline Offline
Newbie Poster

Re: help with dynamic memory

 
0
  #16
May 14th, 2007
>Have I still not answered your question?

You have answered my question. Thanks
I was not looking for a handout if that was the impression.
I want to understand what i am doing. Your code is helpful.
Programming can be frustrating sometimes, and your remarks didnt help at all.....but i do apologize.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 11
Reputation: m3an is an unknown quantity at this point 
Solved Threads: 1
m3an m3an is offline Offline
Newbie Poster

Re: help with dynamic memory

 
0
  #17
May 14th, 2007
Originally Posted by Narue View Post
>Read the first line in the 5th post and then see who should take lessons.
I still think it's you. You see, you had some weird looping problem, but my code worked perfectly. So, since I answered the question you asked, and your broken attempt at my answer didn't work, I didn't answer your question? That's some good logic, and it explains why you can't write working code. Please don't take your incompetence out on me.
I didnt say your code wasnt right....wheres the quote???.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 11
Reputation: m3an is an unknown quantity at this point 
Solved Threads: 1
m3an m3an is offline Offline
Newbie Poster

Re: help with dynamic memory

 
0
  #18
May 14th, 2007
ok thats nice...you really dont deserve an apology..bch
Last edited by m3an; May 14th, 2007 at 7:29 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 21
Reputation: desijays is an unknown quantity at this point 
Solved Threads: 1
desijays desijays is offline Offline
Newbie Poster

Re: help with dynamic memory

 
0
  #19
May 15th, 2007
how did i b'come a part of this !!! gggggggeeeeeeeeesssssssshhhhhhh.
Last edited by desijays; May 15th, 2007 at 11:14 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 11
Reputation: m3an is an unknown quantity at this point 
Solved Threads: 1
m3an m3an is offline Offline
Newbie Poster

Re: help with dynamic memory

 
0
  #20
May 15th, 2007
Originally Posted by desijays View Post
how did i b'come a part of this !!! gggggggeeeeeeeeesssssssshhhhhhh.
that was not for u
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



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC