linked list problem
-
I wrote a linked list to hande some object that I created. When I try to print the list, it prints garbage. I made a temporary project to test my class and it works fine, so the problem is how I am using it in my project. I think that it is a memory issue and that the objects are being written over after I store them in my list. This is how I am using the list. //funtion to create object that will be stored in list. CApproach ApproachPage::ParseApproach(CString string) { ...... CApproach approach(Chart_Name, Chart_Num, Chart_Rev_Date); return approach; } //Insert class in List.cpp int PrintList::Insert(CApproach* approach ) { ..... } //main.h PrintList* list; //contructor of main list = new PrintList(); //main list->Insert(&ParseApproach(codestring)); //prints garbage to the outputfile, but works in another project list->print() I learned to program in java so pointers and c++ memory are far from my specialty. Any help would be great. Thanks. Kevin Shaffer kshaff03@msn.com
-
I wrote a linked list to hande some object that I created. When I try to print the list, it prints garbage. I made a temporary project to test my class and it works fine, so the problem is how I am using it in my project. I think that it is a memory issue and that the objects are being written over after I store them in my list. This is how I am using the list. //funtion to create object that will be stored in list. CApproach ApproachPage::ParseApproach(CString string) { ...... CApproach approach(Chart_Name, Chart_Num, Chart_Rev_Date); return approach; } //Insert class in List.cpp int PrintList::Insert(CApproach* approach ) { ..... } //main.h PrintList* list; //contructor of main list = new PrintList(); //main list->Insert(&ParseApproach(codestring)); //prints garbage to the outputfile, but works in another project list->print() I learned to program in java so pointers and c++ memory are far from my specialty. Any help would be great. Thanks. Kevin Shaffer kshaff03@msn.com
The CApproach object returned from ParseApproach is a temporary object. You're passing the address of this temporary to Insert. When the temporary object is destroyed automatically the pointer to it points to garbage. You can either let Insert take a copy of a CApproach object instead of a pointer:
int PrintList::Insert(CApproach approach)
or you can assign the result of ParseApproach to a local variable, but keep in mind that when that variable goes out of scope, the pointer to it will point to garbage:CApproach result = ParseApproach(codestring); list->Insert(&result); list->print();
You could also pass Insert a pointer to an object allocated on the heap, but you have to remember to delete it when you're done with it. -
I wrote a linked list to hande some object that I created. When I try to print the list, it prints garbage. I made a temporary project to test my class and it works fine, so the problem is how I am using it in my project. I think that it is a memory issue and that the objects are being written over after I store them in my list. This is how I am using the list. //funtion to create object that will be stored in list. CApproach ApproachPage::ParseApproach(CString string) { ...... CApproach approach(Chart_Name, Chart_Num, Chart_Rev_Date); return approach; } //Insert class in List.cpp int PrintList::Insert(CApproach* approach ) { ..... } //main.h PrintList* list; //contructor of main list = new PrintList(); //main list->Insert(&ParseApproach(codestring)); //prints garbage to the outputfile, but works in another project list->print() I learned to program in java so pointers and c++ memory are far from my specialty. Any help would be great. Thanks. Kevin Shaffer kshaff03@msn.com
If this is not a homework assignment you should try and use one of the built-in lists link CList or better yet list. Clist is a MFC template class and list is STL. See the MDSN help for documentation. There really is no good reason to have to create your own linked list when there are very good lists available. John