| | |
help with dynamic memory
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
>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:
Unless the rules have recently changed without my knowing, that's not a correct definition of main. The access is legal.
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)
void Names::enter_new()
I'm here to prove you wrong.
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":
Have I still not answered your question?
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <new> using namespace std; class Names { private: string title; string *p; int i; public: Names ( const string& init ): title ( init ) {} ~Names() { delete[] p; } void enter_new(); void display(); }; void Names::enter_new() { cout<<"How many movies would you like to type? "; cin>> i; cin.ignore(); p = new(nothrow) string[i]; if ( p == 0 ) cout << "Error: memory could not be allocated"; else { for ( int a = 0; a < i; a++ ) { cout<<"Actor "<< a + 1 <<": "; getline ( cin, p[a] ); } } } void Names::display() { cout<<"Title: "<< title <<"\nActors:\n"; for ( int a = 0; a < i; a++ ) cout<<'\t'<< p[a] <<'\n'; } int main() { Names film ( "My film" ); film.enter_new(); film.display(); }
I'm here to prove you wrong.
•
•
Join Date: Mar 2007
Posts: 21
Reputation:
Solved Threads: 1
•
•
•
•
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)
class Names { private: string title; int i; int * p; public: void enter_new(); // sets names of actors to the DVD }film; void Names::enter_new() {char ans; cout << "How many movies would you like to type? "; cin >> film.i; //I was talking about this Narua int ii= film.i; (film.p)= new int[ii]; if ((film.p) == 0) cout << "Error: memory could not be allocated"; else { for (int a=1; a<(ii); a++) {cout<<"\n Please enter the name: "<<endl; cin >> (film.p)[a]; } delete[] p; }
•
•
Join Date: Nov 2006
Posts: 11
Reputation:
Solved Threads: 1
>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.
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.
>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 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'm here to prove you wrong.
•
•
Join Date: Nov 2006
Posts: 11
Reputation:
Solved Threads: 1
>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.
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.
•
•
Join Date: Nov 2006
Posts: 11
Reputation:
Solved Threads: 1
•
•
•
•
>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.
![]() |
Similar Threads
- Dynamic memory for an array of objects (C++)
- Static and Dynamic Memory Allocations...Advan's & Disadvan's??? (Computer Science)
- regarding dynamic memory allocation (C)
- Dynamic memory allocation homework (C++)
Other Threads in the C++ Forum
- Previous Thread: for use from file
- Next Thread: Is it a problem due to scope or compiler ?
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






