0

I've made a Class with name Matrix. This class has two constructor, one default constructor and one copy constructor. In addition there is a public method called Determinant .In main I write :

Matrix a(); 
a.Determinant()

or

Matrix a(); 
a->Determinant()

But none of them is valid. I print the type of the a but I can't understand what type is this ?

  • 1
    Possible duplicate of [Default constructor with empty brackets](https://stackoverflow.com/questions/180172/default-constructor-with-empty-brackets) – Zalman Stern Oct 24 '17 at 16:14

2 Answers2

0

This is C++'s most vexing parse. The line:

Matrix a(); 

Is interpreted as a declaration of a function that accepts no arguments and returns a Matrix. The proper way to create a default constructed object is simply

Matrix a;
Nasser Al-Shawwa
  • 3,573
  • 17
  • 27
  • The error at the two examples is not at line 1 but at line 2. So, why ? – chaviaras michalis Oct 24 '17 at 16:27
  • 1
    @chaviarasmichalis Yes, this is the expected behavior. Line 1 is correctly declaring the function `a`, but then you are trying to call the `a.Determinant()` method. Functions in C++ don't have methods. If you implement the correction in the answer, the error in line 2 should go away. – Nasser Al-Shawwa Oct 25 '17 at 08:40
0

The line:

Matrix a();

declares a function returning a Matrix, not an object with default construction. Omit the parentheses.

See: https://en.wikipedia.org/wiki/Most_vexing_parse for discussion of a more difficult case. (This has to be a duplicate, but a quick search didn't turn one up. I'll look harder in a minute.)

Zalman Stern
  • 3,161
  • 12
  • 18