may be undefined" When I use operator overloading [] [] in C ++ to create a minimum matrix class class matrix { priva...">

Warning "in <variable> may be undefined"

When I use operator overloading [] [] in C ++ to create a minimum matrix class

class matrix {
private:
  vector<T> elems_;
  size_t         nrows_;
  size_t         ncols_;  
public:
  T const* operator[] ( size_t const r ) const { return &elems_[r * ncols_]; }
  T*       operator[] ( size_t const r )       { return &elems_[r * ncols_]; }  
  matrix ();
  matrix ( size_t const nr, size_t const nc ) 
    : elems_( nr * nc ), nrows_( nr ), ncols_( nc ) 
  { }
  matrix ( size_t const nr, size_t const nc, T const *data) 
    : elems_( nr * nc ), nrows_( nr ), ncols_( nc ) 
  { size_t ptr=0; 
    for (int i=0;i<nr;i++) 
        for (int j=0;j<nc;j++) 
             elems_[ptr] = data[ptr++]; 
  }
}

g ++ returns a warning operation on β€˜ptr’ may be undefined [-Wsequence-point]. In a previous post Why did I get β€œoperation may be undefined” in Statement statement in C ++? explained that the last thing in the compound statement should be an expression followed by a semicolon, but not what purpose does it serve. Does this mean that g++this warning will always be thrown for compound statements that have a loop end at the end?

If anyone can explain why this is helpful? I can't think of any reason why someone should be warned about terminating a compound statement with a loop for.

+3
1

elems_[ptr] = data[ptr++], undefined, elems_[ptr] data[ptr++]. , = .

elems_[ptr] = data[ptr++] . .

+5

All Articles