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
599 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 1

Assisted Solution

by:mifergie
mifergie earned 167 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 167 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Assisted Solution

by:w00te
w00te earned 166 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

730 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