title: Dot product implementation in C++
body: <p>I am struggling to get my method to work. I am a beginner in C++. I am trying to find a dot product of two vectors. I am getting a few errors in the <em>for</em> loops. How can I fix them?</p>
  <pre><code>float dot(Matrixf const&amp; vec1, Matrixf const&amp; vec2) {
  
      // error check
      if (!vec1.isVector() || !vec2.isVector()) {
          throw std::runtime_error(&quot;Unable to do dot product: not column vectors.&quot;);
      }
      if (vec1.nrows() != vec2.nrows()) {
          throw std::runtime_error(&quot;Unable to do dot product: vector lengths not equal.&quot;);
      }
  
      /** implementing dot product *************************************/
  
      float ret = 0;
  
      for(unsigned i = 0; i &lt; vec1.ncols(); ++i){
          for(unsigned j =0; j&lt; vec2.nrows(); ++j){
              ret += vec1[i] * vec2[j];
          }
      }
      return ret;
  }
  </code></pre>
  <p>Matrixf class</p>
  <pre><code>#include &quot;matrixf.h&quot;
  
  #include &lt;iostream&gt;
  
  Matrixf::Matrixf(unsigned int rows, unsigned int cols) {
      rows_ = rows;
      cols_ = cols;
      data_ = new float[rows_ * cols_];
  
      // set all initial values to zero
      for (unsigned int r = 0; r &lt; rows_; ++r) {
          for (unsigned int c = 0; c &lt; cols_; ++c) {
              data_[r * cols_ + c] = 0;
          }
      }
  }
  
  Matrixf::~Matrixf() {
      delete data_;
  }
  
  Matrixf::Matrixf(Matrixf const&amp; other) {
      rows_ = other.rows_;
      cols_ = other.cols_;
      data_ = new float[rows_ * cols_];
      for (unsigned int i = 0; i &lt; rows_ * cols_; ++i) {
          data_[i] = other.data_[i];
      }
  }
  
  Matrixf&amp; Matrixf::operator=(Matrixf const&amp; other) {
      // handles self assignment
      if (this == &amp;other) {
          return *this;
      }
  
      delete data_;
      rows_ = other.rows_;
      cols_ = other.cols_;
      data_ = new float[rows_ * cols_];
      for (unsigned int i = 0; i &lt; rows_ * cols_; ++i) {
          data_[i] = other.data_[i];
      }
      return *this;
  }
  
  float Matrixf::get(unsigned int row, unsigned int col) const {
      #ifndef NDEBUG
          if (row &gt;= rows_ || col &gt;= cols_) {
              throw std::runtime_error(&quot;Matrix index out of bounds.&quot;);
          }
      #endif
  
      return data_[row * cols_ + col];
  }
  
  void Matrixf::set(unsigned int row, unsigned int col, float val) {
      #ifndef NDEBUG
          if (row &gt;= rows_ || col &gt;= cols_) {
              throw std::runtime_error(&quot;Matrix index out of bounds.&quot;);
          }
      #endif
      data_[row * cols_ + col] = val;
  }
  
  float&amp; Matrixf::operator()(unsigned int row, unsigned int col) {
      return data_[row * cols_ + col];
  }
  
  float Matrixf::operator()(unsigned int row, unsigned int col) const {
      return data_[row * cols_ + col];
  }
  
  unsigned int Matrixf::nrows() const {
      return rows_;
  }
  
  unsigned int Matrixf::ncols() const {
      return cols_;
  }
  
  bool Matrixf::isVector() const {
      return (cols_ == 1);
  }
  
  Matrixf Matrixf::eye(unsigned int size) {
      Matrixf e(size, size);
      for (unsigned int i = 0; i &lt; size; ++i) {
          e.set(i, i, 1);
      }
  
      return e;
  }
  
  std::ostream&amp; operator &lt;&lt; (std::ostream&amp; os, Matrixf const&amp; matrix) {
      for (unsigned int r = 0; r &lt; matrix.nrows(); ++r) {
          for (unsigned int c = 0; c &lt; matrix.ncols(); ++c) {
              os &lt;&lt; matrix.data_[r * matrix.cols_ + c] &lt;&lt; &quot; &quot;;
          }
          os &lt;&lt; &quot;\n&quot;;
      }
      return os;
  }
  </code></pre>