Solved

How do I modify a C++ class to display which variable is uninitialized?

Posted on 2010-11-13
8
388 Views
Last Modified: 2012-05-10
How do I modify a C++ class to display which variable is unitialized?  My program accepts an expression and checks, among other things, that all variables are initialized.  Currently, it throws an error when there is an unitialized variable, but how to I get it to display that variable that is uninitialzed?

Example:

I enter: (x + y), x=2;

It throws an exception and displays:
Error: Uninitialzed variable.

I want it to display which variable is uninitialized:

Error: Uninitialized variable y
main.cpp
symboltable.cpp
variable.cpp
subexpression.cpp
0
Comment
Question by:NSing9
  • 5
  • 3
8 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
Comment Utility
Make your exception have a string constructor
Then do
	if (symbolTable.lookUp(name) == -1)		//
		throw UninitializedException(name);		// terminate function

Open in new window


You didn't post see the code for UninitializedException, but I think you can figure it out from there.
0
 

Author Comment

by:NSing9
Comment Utility
I'm not sure how to do that.
0
 
LVL 37

Expert Comment

by:TommySzalapski
Comment Utility
Let me see the UninitializedException code.
0
 

Author Comment

by:NSing9
Comment Utility
See attached.
UninitializedException.h
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 37

Accepted Solution

by:
TommySzalapski earned 250 total points
Comment Utility
Try this.
#include <stdexcept> // stdexcept header file contains runtime_error

#include <string>

using std::runtime_error; // standard C++ library class runtime_error

using std::string



class UninitializedException : public runtime_error

{

public:

	// constructor specifies default error message

	UninitializedException::UninitializedException()

		: runtime_error( "Uninitialized Variable" ) {}

	UninitializedException::UninitializedException(string name)

		: runtime_error( "Uninitialized Variable: " + name ) {}



}; // end class UninitializedException

Open in new window

0
 
LVL 37

Expert Comment

by:TommySzalapski
Comment Utility
Actually, the line probably needs to be

: runtime_error( ("Uninitialized Variable: " + name).c_str() ) {}

Open in new window

0
 
LVL 37

Expert Comment

by:TommySzalapski
Comment Utility
If name isn't a std::string type you'll need to adjust of course.
0
 

Author Closing Comment

by:NSing9
Comment Utility
Thank you!  It works after modifying the constructor.  Although I don't know C++ well, some of the basic programming concepts have escaped me after being away from it for so long.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

771 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

12 Experts available now in Live!

Get 1:1 Help Now