Solved

How do I modify a C++ class to handle unitialized variables?

Posted on 2010-11-12
9
352 Views
Last Modified: 2012-05-10
How do I modify the  attached C++ class to handle unitialized variables?  It is a symbol table and I want it to display an error such as "Uninitialized Variable y" for expressions like:
(x/y), x = 1;

#include <string>
#include <vector>
using namespace std;

#include "symboltable.h"

void SymbolTable::insert(string variable, double value)
{
    const Symbol& symbol = Symbol(variable, value);
    elements.push_back(symbol);
}

double SymbolTable::lookUp(string variable) const
{
    for (int i = 0; i < elements.size(); i++)
		if (elements[i].variable == variable)
			return elements[i].value;
    return -1;
}

Open in new window

0
Comment
Question by:NSing9
  • 5
  • 3
9 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 34126816
Right now, your SymbolTable::lookUp function is returning -1 when the symbol is not found.

If you want to generate an exception, here are two options :

(a) let SymbolTable::lookUp throw an exception instead of returning -1.

(b) let the calling code throw an exception when -1 is returned.

Either way, make sure to catch the exception somewhere, and handle it appropriately (by displaying an error message eg.).

If you have specific issues with how exceptions are implemented in code, here's a nice basic tutorial about them :

        http://www.cplusplus.com/doc/tutorial/exceptions/

If your problem is something else, please let us know what exactly.
0
 

Author Comment

by:NSing9
ID: 34127695
Thanks.  I think that is where the problem lies.  I don't know how to implement that portion of the code.  I have read the tutorial about exceptions a few time already, but still can't seem to implement the correct code.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34127962
Could you post what you tried here ? That way, it'll be easier for me to see where the problem lies.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:NSing9
ID: 34128141
Yes, here are the four files that I have been working with.  I have tired making the changes in both the main class and variable class.
Test.cpp
DivideByZeroException.h
symboltable.cpp
variable.cpp
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 300 total points
ID: 34128162
That's mostly looking good.

All that is missing, is a catch block for exceptions of the type UninitializedException (just like the catch block you already have for exceptions of the type DivideByZeroException).

Btw, you didn't post the UninitializedException.h file, but I assume it's very similar to the DivideByZeroException.h file.

If you still get errors or other problems, could you please post the exact error message(s) here ?
0
 
LVL 8

Expert Comment

by:davesgonebananas
ID: 34128175
You're calling throw UninitializedException() where that looks to be a class. You probably need to create an object of type UnitializedException and throw that instead.

throw new UnitializedException();
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34128202
>> throw new UnitializedException();

I would NOT recommend throwing a pointer - especially not a pointer to dynamically allocated memory (that can cause all kinds of issues - eg. if the cause of the exception was an out-of-memory condition).

Instead, throw objects whenever you can (and catch them by reference in order to avoid unnecessary copies).

So, the current code is just fine.
0
 

Author Closing Comment

by:NSing9
ID: 34128276
It finally worked.  I had too many windows open in my compiler.  I had a catch block for exceptions of the type UninitializedException but I had entered it in the wrong file.  I also had a UninitializedException.h, but I attached the wrong one to my comments.  It is working perfectly now.  Thanks for your help!
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34130047
Great :)
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
is twain_32.dll cmpatible with windows 10 ? 10 156
How to access ANSI/IEEE Std 754 or equivalent information ? 3 63
How to learn Linux? 10 60
nested if statement in excel help 4 26
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question