title: Creating 2-D Associative Array Using Template Operator
body: <p>Trying to define 2-D Associative Array, I wrote the template operator inside my class to make it closer to original array. This is my operator function.</p>
  
  <pre><code>template &lt;class T, class U, class V&gt;
  T&amp; TwoAssocDArray&lt;T,U,V&gt;::operator [](U input1)[](V input2) {
      for(int i=0; i&lt;size(); i++)
          if(stack[i].key1 == input1 &amp;&amp; stack[i].key2 == input2) 
                  return stack[i].d;
      DataPack pack;
      pack.key1 = input1;
      pack.key2 = input2;
      stack.push_back(pack);
      return stack[size].d;
  }
  </code></pre>
  
  <p>Class Interface,</p>
  
  <pre><code>#ifndef _DATASTRUCTURE_H_
  #define _DATASTRUCTURE_H_
  
  #include &lt;vector&gt;
  
  using namespace std;
  
  template &lt;class T, class U, class V&gt;
  class TwoDAssocArray {
  
  private:
      typedef struct _DataPack {
          T d;
          U key1;
          V key2;
      } DataPack;
      vector&lt;DataPack&gt; stack;
  
  public:
      int size();
      bool isIn(U input1, V input2);
      bool add(T data, U input1, V input2);
      T get(U input1, V input2);
      T&amp; operator [](U input1)[](V input2);
  };
  
  #endif
  </code></pre>
  
  <p>Then I tested it in main().</p>
  
  <pre><code>int main(int argc, char *argv[]) {
  
      TwoDAssocArray&lt;int, char*, char*&gt; assocArr;
  
      assocArr["in1"]["in2"] = 2246001;
      cout &lt;&lt; assocArr["in1"]["in2"] &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>
  
  <p>After build and run, I found it didn't work, got a dozen of errors.
  I assumed the main reason is template syntax with which I am not familiar.
  I believe that a ton of errors always come from just a few syntax errors.</p>
  
  <p>If anyone would help me, I would appreciate.</p>