• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 362
  • Last Modified:

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

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
NSing9
Asked:
NSing9
  • 5
  • 3
1 Solution
 
Infinity08Commented:
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
 
NSing9Author Commented:
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
 
Infinity08Commented:
Could you post what you tried here ? That way, it'll be easier for me to see where the problem lies.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
NSing9Author Commented:
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
 
Infinity08Commented:
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
 
davesgonebananasCommented:
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
 
Infinity08Commented:
>> 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
 
NSing9Author Commented:
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
 
Infinity08Commented:
Great :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now