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

Posted on 2010-11-12
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);



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

Question by:NSing9
  • 5
  • 3
LVL 53

Expert Comment

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 :

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

Author Comment

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.
LVL 53

Expert Comment

ID: 34127962
Could you post what you tried here ? That way, it'll be easier for me to see where the problem lies.

Author Comment

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.
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

LVL 53

Accepted Solution

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 ?

Expert Comment

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();
LVL 53

Expert Comment

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.

Author Closing Comment

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!
LVL 53

Expert Comment

ID: 34130047
Great :)

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now