|
-
April 23rd, 2013, 07:40 AM
#4
Re: [RESOLVED] Trees (Maps) problem
There are a few issues with your code:
1) Although this does not affect your program, you
should not put "using namespaec std;" in your
header file. It can cause name clash problems.
You should fully qualify the variable type. Example:
Code:
void print(std::map<std::string, int> &tree);// print
2) print() does not modify any variable in the Map class.
Therefore it should be a const function:
Code:
void print(std::map<std::string, int> &tree) const; // print
3) additionally, print does not modify the passed argument,
so you should pass by const reference, not by reference.
You would need to use const_iterator instead of iterator
in the function.
Code:
void print(const std::map<std::string, int> &tree) const; // print
4) in main, you call:
Code:
mapper.print(words);
But words is a local variable to main that is never modified.
That is why you did not see any output.
5) in your header, you comment out the line:
Code:
//map<string, int> words;
And instead you have "words" a local variable in the inputfile
function. I think you should keep the member variable words
and remove the local variable from inputfile. You would not
need to pass the map to print() and insertTree. Just make
sure those functions use the member variable, words.
6) There is nothing wrong with the way you coded insertTree,
but it can be simplified to one line (but it requires a little
more advanced knowledge on how operator [] works for std::map).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|