Note that there are some explanatory texts on larger screens.

plurals
  1. PODefining maps in class
    text
    copied!<p>I am new to C++ and am not able to correctly define maps in class definition.</p> <p>Here is the code I have written:</p> <pre><code>#include&lt;stdio.h&gt; #include &lt;iostream&gt; #include &lt;map&gt; #include &lt;utility&gt; // make_pair #include &lt;string.h&gt; #include &lt;sstream&gt; using namespace std; class assgnt { private: //typedef std::map&lt;int, int&gt; MapType; //MapType my_map; public: bool isGoodPoint(int x, int y); void addGoodNeighbours(int x, int y); }; inline bool assgnt::isGoodPoint(int x, int y) { string xs, ys; stringstream xout, yout; int xsum=0, ysum=0; xout&lt;&lt;x; yout&lt;&lt;y; xs = xout.str(); ys = yout.str(); for each (char c in xs) { xsum = xsum + int(c); } for each (char c in ys) { ysum = ysum + int(c); } if (xsum+ysum &lt;= 19) return true; else return false; } inline void assgnt::addGoodNeighbours(int x, int y) { //if assgnt::isGoodPoint(x+1,y) if(isGoodPoint(x+1,y)) { } /*if isGoodPoint(x+1,y) and [x+1,y] not in points: points.append([x+1,y]) if isGoodPoint(x-1,y) and [x-1,y] not in points: points.append([x-1,y]) if isGoodPoint(x,y+1) and [x,y+1] not in points: points.append([x,y+1]) if isGoodPoint(x,y-1) and [x,y-1] not in points: points.append([x,y-1])*/ } int main() { typedef std::map&lt;int, int&gt; MapType; MapType my_map; // insert elements using insert function my_map.insert(std::pair&lt;int, int&gt;(0, 0)); int i=0; while true { //my_map.insert(std::pair&lt;int, int&gt;(2, 2)); //my_map.insert(std::pair&lt;int, int&gt;(3, 3)); //my_map.insert(MapType::value_type(4, 4)); // all standard containers provide this typedef //my_map.insert(std::make_pair(5, 5)); // can also use the utility function make_pair MapType::iterator iter = my_map.begin(); std::cout &lt;&lt; "Size of my_map: " &lt;&lt; my_map.size() &lt;&lt; '\n'; std::cout &lt;&lt; "Enter a key to search for: "; int c; std::cin &gt;&gt; c; iter = my_map.find(c); if (iter != my_map.end()) { int num = iter-&gt;second; if(num == 0) std::cout &lt;&lt; "Value is found: " &lt;&lt; iter-&gt;second &lt;&lt; '\n'; else std::cout &lt;&lt; "Value not found: "; } else std::cout &lt;&lt; "Key is not in my_map" &lt;&lt; '\n'; // my_map.clear(); } </code></pre> <p>I wish to define the map typedef std::map MapType; in the class definition, so that I can use it in all inline functions and the main() by creating its object.</p> <p>Please help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload