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
595 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 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
 
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
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

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 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

14 Experts available now in Live!

Get 1:1 Help Now