![]() |
|
#1
|
|||
|
|||
outputting a class from a linked listi am writing a program to perform various functions on some inputted shapes (for a course im taking) and have hit a bit of a brick wall...
was wondering if anyone here could help. my problem is this; i have a list of rectangles stored in a linked list and need to output the details of said rectangles using a display list function. within the the display list function i would like to access either the "id" of the shape (that being an arbitart counter used to identify the shape) or print out all the details of the shape using a showinfo() function. just when i thought i was making progress i got a random break/error when i came to displaying the list. the debugger just said "unspecified error". CPP / C++ / C Code:
CPP / C++ / C Code:
as far as im aware this is the only problem currently, though im far from finished. if you need copies of anything else please let me know. oh, my showinfo looks like: CPP / C++ / C Code:
any help appreciated thanks Sam edit: highlighted where my problems are, as i put quite a lot of code up there |
|||
|
#2
|
|||
|
|||
Re: outputting a class from a linked listQuote:
When you add a rectangle to the list, here is what I see: 1. In createrect, you create a rect object named temp2, The object's scope is local to that function. 2. You send the address of this object to appendNode 3. In appendNode you create a ListNode struct and set its value member equal to the address of the temporary object whose address the appendNode function received. 4. The new node is appended to the list. 5. After appendNode returns, you get user information for width and height and store them in the temporary struct that is pointed to by the last element of the list. 6. When the function createrect returns, the temporary struct goes out of scope, so the pointer to it no longer points to valid memory. 7. After leaving createrect you try to print out values of some things that are not in valid memory. The result is undefined behavior, and that means that just about anything could happen. Successive program runs could give different results, depending on what else is happening in your system. In other words: Why the heck did you make a linked list of [i]pointers[/I to rects? Why not just make a linked list of rects? You could have the head pointing to a rect object (Instead of having it point to a pointer to a rect). Each rect object would have a pointer member that points to the next rect in the list. Now, I could be missing something (but I have already said that). However, if, for some reason, you absolutely have to have a list of pointers, then you had better make damn sure that each pointer actually points to something in valid memory. Regards, Dave |
|
#3
|
|||
|
|||
Re: outputting a class from a linked listhmm, i was wondering why i get nothing when i try to output it... im not very good with pointers, i find them to be, shall we say, one of the more challenging aspects of programming.
ill have a jiggle around with them see if it helps. will be tomorrow now though as i dont have access to much in the way of a compiler at home. thanks for your help ill post again tomorrow and let you know how i get on. Sam |
|
#4
|
|||
|
|||
Re: outputting a class from a linked listok iv had a quick jiggle about with the createrect function and think iv kinda got that sorted, i just dont quite know how to change my appendnode function now to let it take actual objects... i copied most of linkedlist.h from a txt book and the explanation wasnt particulary good. i now get
error C2664: 'LinkedList<T>::appendNode' : cannot convert parameter 1 from 'rect *' to 'rect' with [ T=rect ] and error C2664: 'LinkedList<T>::findNode' : cannot convert parameter 1 from 'rect *' to 'rect' with [ T=rect ] error messages. here's what the relevant code looks like now CPP / C++ / C Code:
with linkedlist.h and the rest of my main the same as before. any help is appreciated. thanks again Sam edit, added some more of my code in, specifically the bits that display and findlist |
|
#5
|
|||
|
|||
Re: outputting a class from a linked listim also having trouble with the output still. something is wrong with my displaylist function
CPP / C++ / C Code:
i only get these error messages if i comment out the findNode bits in my main which are still throwing up cannot convert from object to pointer errors. error C2819: type 'rect' does not have an overloaded member 'operator ->' error C2227: left of '->ShowInfo' must point to class/struct/union as far as i can see (unless im missing something) rect has a member function of showinfo and i dont think iv ever overloaded a pointer opperator... so i dont quite know how to go about doing that. |
|
#6
|
|||
|
|||
Re: outputting a class from a linked listi understand that im trying to output a rect class using this but i thought that the fact it was a template would solve the problem... cant seem to fix it. iv tried all my usual tricks with no luck...
|
|
#7
|
|||
|
|||
Re: outputting a class from a linked listQuote:
If you have a pointer to an object, you access its members with -> If you have an object, you acces its members with. (a dot) Regards, Dave |
|
#8
|
|||
|
|||
Re: outputting a class from a linked listok, i kinda started this again and came at it from a differant angle, solving my problems for the most part.
BUT im now having trouble getting the program to store my date. once its entered it is appended to the list and that works fine, but im trying to come up with a semi-decent menu system that allows me to delete shapes, edit shapes etc when i break out of the loop that allows me to input the shapes, the data is lost (goes out of scope??) here's my main file (which as far as i can see is where the problem lies) thanks for the previous posts btw, they did help for a time. CPP / C++ / C Code:
any advice would be welcommed. im wondering if it's because iv used the goto function, as it goes back to the start of the program... if it is, any suggestions as to how else to do it would be good as iv tried using break and it just seems to end my program. i have also tried using a switch system but i just cant seem to get it to work as once again when i break out of the loop it loses all the data. thanks again. Sam p.s. i should point out that the rest of my code DOES work, and if i put my displaylist inside the create part of the program it works. |
Recent GIDBlog
Install Adobe Flash - Without Administrator Rights by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Str_Misaligned in Double Link List | Peter_APIIT | C Programming Language | 1 | 29-Feb-2008 20:50 |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 07:44 |
| Hard drive/CPU Diagnoses Issues | binarybug | Computer Hardware Forum | 1 | 22-Jan-2007 19:23 |
| C++ class -- Please help | vnca_1 | C++ Forum | 3 | 14-Jun-2006 12:31 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The