3

I am writing a template class of a matrix named Matrix, and I rewrite the default constructor like this:

template<typename _Tp, size_t m, size_t n> inline
Matrix<_Tp, m, n>::Matrix()
{
    for(size_t i = 0; i != m*n; ++i) val[i] = _Tp(0);
}

And in my test file I write this:

SC::Matrix<double, 3, 3> Mat();

All these are good when I build the program. But I always get wrong result when I run the test program.

And When I try to find reasons I find that the debugger always skip over definition of Mat; At the first I think that it may because I modified the files after I build this program, so I delete all the build results(automatically generated by cmake) and rebuild it. But it's useless, the problem is still there.

Is there anyone can help me find the reason? Did I provide enough information for this problem?

X.S. Wang
  • 217
  • 2
  • 8
  • 4
    Is that a definition of a variable `Mat` that is supposed to be default initialized, or is it a declaration of a function which takes no argument and returns a `SC::Matrix` object? – Some programmer dude Aug 25 '17 at 07:53
  • 3
    When you construct objects on the stack without ctor-params you don't need the `()`: `SC::Matrix Mat;`. Otherwise it may cause your compiler to think of it as a function declaration. – Al.G. Aug 25 '17 at 07:54
  • Thanks a lot. I got the reason now. – X.S. Wang Aug 25 '17 at 08:01

1 Answers1

7

You say "...the debugger always skip over...", so I assume, you tried to create a variable Mat of type SC::Matrix<double, 3, 3> and see how it is default initialized.

If that is true, than

SC::Matrix<double, 3, 3> Mat();

declares a function called Mat taking no args and returning SC::Matrix<double, 3, 3>. And of course you can not "debug" a function declaration. If you want to create a default initialized variable write:

SC::Matrix<double, 3, 3> Mat{};

or just

SC::Matrix<double, 3, 3> Mat;
WindyFields
  • 2,697
  • 1
  • 18
  • 21