943,614 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2805
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
May 14th, 2007
0

Re: help with dynamic memory

>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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 14th, 2007
0

Re: help with dynamic memory

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":
C++ Syntax (Toggle Plain Text)
  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?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 14th, 2007
0

Re: help with dynamic memory

Click to Expand / Collapse  Quote originally posted by m3an ...
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


C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 1
Newbie Poster
desijays is offline Offline
21 posts
since Mar 2007
May 14th, 2007
0

Re: help with dynamic memory

>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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
m3an is offline Offline
11 posts
since Nov 2006
May 14th, 2007
0

Re: help with dynamic memory

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 14th, 2007
0

Re: help with dynamic memory

>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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
m3an is offline Offline
11 posts
since Nov 2006
May 14th, 2007
0

Re: help with dynamic memory

Click to Expand / Collapse  Quote originally posted by Narue ...
>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???.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
m3an is offline Offline
11 posts
since Nov 2006
May 14th, 2007
0

Re: help with dynamic memory

ok thats nice...you really dont deserve an apology..bch
Last edited by m3an; May 14th, 2007 at 7:29 pm.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
m3an is offline Offline
11 posts
since Nov 2006
May 15th, 2007
0

Re: help with dynamic memory

how did i b'come a part of this !!! gggggggeeeeeeeeesssssssshhhhhhh.
Last edited by desijays; May 15th, 2007 at 11:14 am.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
desijays is offline Offline
21 posts
since Mar 2007
May 15th, 2007
0

Re: help with dynamic memory

Click to Expand / Collapse  Quote originally posted by desijays ...
how did i b'come a part of this !!! gggggggeeeeeeeeesssssssshhhhhhh.
that was not for u
Reputation Points: 10
Solved Threads: 1
Newbie Poster
m3an is offline Offline
11 posts
since Nov 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: for use from file
Next Thread in C++ Forum Timeline: Is it a problem due to scope or compiler ?





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


Follow us on Twitter


© 2011 DaniWeb® LLC