?
Solved

How do I modify a C++ class to display a warning for variables that are initialized but never used?

Posted on 2010-11-13
8
Medium Priority
?
641 Views
Last Modified: 2012-08-13
How do I modify a C++ class to display a warning for variables that are initialized but never used?  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 do I get it to display a warning for a variable that is not used and display that variables name in the output?

Example:

I enter: (2/3), x = 1;

It should display a warning along with the variable name and correct value:
Warning: Unreferenced Variable x Value = 0.666667

Right now it is only displaying:  Value = 0.666667

I have included a few of the classes that I have created so far but can't figure out how to add this piece to warn when unreferenced variables are encountered.  I have also include the UninitializedException.h file to show how I currently throw an exception for the uninitialized variables.
symboltable.cpp
main.cpp
variable.cpp
UninitializedException.h
0
Comment
Question by:NSing9
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 1

Assisted Solution

by:mifergie
mifergie earned 668 total points
ID: 34129217
You have a list of variables that you are assigning values to.  If you add an element to that class that contains "referenced" info, then initialize that value to 0.  You can scan the array and look for any 0s and then send those out to the user as unreferenced.  The code is simple.
0
 

Author Comment

by:NSing9
ID: 34129226
It sounds easy, but I have been stuck on this portion for quite sometime.  It's simple to an expert, but to a novice like myself, I have been at it for a few hours.
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 668 total points
ID: 34131504
mifergie's approach is good, but might need a bit more specific guidance :

Right now, a Symbol consists of a variable name and a value. If you add to that a boolean flag that indicates whether the variable has been referenced, and initialize it to false, you can then set it to true whenever the SymbolTable::lookUp function is called for that variable (ie. it's being referenced).

When the entire input is parsed, you can then check all entries in the symbol table whether the flag is set, and display a warning message if it's false.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 12

Assisted Solution

by:w00te
w00te earned 664 total points
ID: 34131536
Well, first of all you might look into using a std::map instead of a std::vector for the symbol table.  A std::map could implicitly hold both values you need - string and double... for example.

std::map<std::string, float> sMap;
sMap.insert(std::make_pair("x", 4.85));

Oh, and you don't have to do the std:: everywhere if you just do "using namespace std;" at the top, but its a personal preference thing.

Regardless of all that though... You aparently have a way of parsing your expression already.  I can't see all of your code as it isn't here, but I'm assuming that your evaluation mechanism uses the symbol table's "double SymbolTable::lookUp(string variable) const" function to retrieve the value assigned to a variable before using it in the expression evaluation.

If you want the simplest solution to what you currently have, ignore the map stuff above and add a field to the Symbol class you're using which is boolean called "referenced".

Ensure that the value of "referened" defaults to false prior to doing evaluation.  Make the lookUp function set the "referenced" variable to true when the lookup is called on that variable.

Upon completion of evaluation you can cycle through the symbol vector and print your warning for all variables which still have referenced set to false.

Hope that helps :)

-w00te
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34131539
lol. How about that for synchronization :)
0
 
LVL 12

Expert Comment

by:w00te
ID: 34131563
Haha, you always beat me by about 30 seconds. Maybe I should stop adding a signature to speed up :p
0
 

Author Comment

by:NSing9
ID: 34132929
Gentlemen, both your explanations make perfect since, but I am stuck on the coding portion.  I have never program in C++ and seem to be struggling with the basic concepts.  Are there any suggestions with the required code to make this work?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34134114
I'm not sure how to make the suggestions clearer, but obviously something is missing in our explanations.

Could you show us where you are stuck ?
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Question has a verified solution.

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
Progress
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

601 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