![]() |
|
#1
|
|||
|
|||
Run-time ErrorHi im pretty new to c++. I'm writing a small program to get myself familiar with pointers and dynamic memory allocation, but ican't figure out whats wrong with my code. get this error at run-time "Unhandled exception at 0x698fe138 (msvcr80d.dll) in CMultQuizDMA.exe: 0xC0000005: Access violation reading location 0xccccccc0."
CPP / C++ / C Code:
any help will be appreciated |
|
#2
|
|||
|
|||
Re: Run-time errorYou have quite a few problems with your code. First, and most immediately obvious is that main must return an int in C++.
Your problem is in how you are declaring your private Question and Answers members and how you are using then in your overloaded (args) constructor. You seem to be "protecting" yourself from the possibility that Question and/or Answer may come to you "already used" in some previous manner. Think about it, when is the only time that your ctor can be used? The member pointers are uninitialized but not NULL and they were not dynamically allocated so they will immediately blow up. Another thing, but much less obvious is that you are "grading on a curve." 3 out of 5 is not too bad for a C student? When is a '60' a passing grade? One has to achieve 4 out of 5 to obtain a "low B." I think that what you need to do is to step through your code with a debugger and check the values of each variable that you have in your code as you step. See what your code does to each of these--particularly the pointer types--and then see what happens as you step. As you step, the "next line" that you get to will "blow up" in various places. That is a clue as to what you're doing wrong. Here are some changes that may help you: CPP / C++ / C Code:
There are some other "finer points" to C++ design and object architecture that you are (expectedly) missing. For example, your code tightly integrates user I/O with the CQuestion class. In using good object-oriented practices, we would expect that Question would represent everything that ISA question. The answer could arguably be said to have NOTHING to do with the implementation of a "Question" class. Also, but probably not something you're ready yet to consider is that in OOP, we would want our Question class to be able to be used ANYWHERE a Question was needed...even is situations such as a "rhetorical" question. Once you better understand the basics, you'll be ready to move on to some of the finer points of quality class design. However, between now and then, you may want to consider ways that you can separate user I/O from those classes that "naturally" are "holders" of data. It wouldn't be unrealistic to think that perhaps your class would be better implemented as a Q&A class, with a legal name. Something like a 1:1 Q:A in the class as members. You see, it is "kinda funny" when we ask ourselves WHAT IS A CQuestion and part of how it is implemented in this case is having 3 wrong answers. What about all of the other possible wrong answers? Aren't they somehow related to CQuestion in someway too? Hopefully I haven't done more to confuse you than help clarify... :davis: |
|
#3
|
|||
|
|||
Re: Run-time errorThanks davis the problem was where you said when i removed the checking in the constructor that my pointers were already pointing to allocated memory it ran with no errors.
I see what your saying about my implementation of a question class, this is part of a college course i'm taking and is mean't to be very specific to multiple choice questions with four possible answers. Thanks for your help, was greatly apprieciated. |
|
#4
|
|||
|
|||
Re: Run-time errorQuote:
With that, then the "name" for your class is "wrong." What the "right" name is eludes me...but something like CQnAGame might be more appropriate... Generally speaking, junior-level college courses (their instructors?) tend toward teaching fundamental skills at the expense of good "reusable code" traits. In some ways I can understand the challenges of teaching the basics. It is sort of like applying grease to your car brakes so that they don't squeal when the instructor is solely focused on teaching you how to depress the brake pedal. Something ain't right...but at least they taught you how to push the pedal down. ...let the "next guy" teach you about quality friction surface maintenance...too complicated for we "fundamentals" guys. Maybe I just expect too much, as fakepoo once pointed out? Quote:
If a 60% grade is failing, at what level of "perfection" is an 80% grade? At what standard does one confer an 'A' to a grade recipient? Is it 'A' worthy if the core of the lesson is successfully implemented at the expense of "other areas" of good coding practices? ...oh well, I've strayed a bit far from the topic on this reply. :davis: |
Recent GIDBlog
Flickr uploads of IA pictures by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Major newbie problem | cynack | MS Visual C++ / MFC Forum | 1 | 08-Apr-2007 11:25 |
| Winsock error when compiling FLTK 2.0 Projects | mauriciorossi | FLTK Forum | 3 | 16-Aug-2005 10:18 |
| Help with syntax errors | PeteGallo | C Programming Language | 7 | 08-Aug-2005 20:30 |
| What is "Ambigious symbol" ??*( a compilation error) | small_ticket | C++ Forum | 2 | 07-Jan-2005 21:10 |
| Can enum have same name as class? | crystalattice | C++ Forum | 3 | 08-Dec-2004 16:43 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The