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?

3
  • 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<double, 3, 3> object? Commented Aug 25, 2017 at 7:53
  • 3
    When you construct objects on the stack without ctor-params you don't need the (): SC::Matrix<double, 3, 3> Mat;. Otherwise it may cause your compiler to think of it as a function declaration. Commented Aug 25, 2017 at 7:54
  • Thanks a lot. I got the reason now. Commented Aug 25, 2017 at 8:01

1 Answer 1

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; 
Sign up to request clarification or add additional context in comments.

2 Comments

It is actually function declaration, not definition. You can debug a function definition (this is what you mostly debug, actually), but not declaration.
Thank you, fixed)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.