2

Can you explain why this won't compile:

(this is the error:

../Man.cpp:33:9: error: conversion from ‘Man (*)()’ to non-scalar type ‘Man’ requested)

Code:

Man goo(){
  Man m();
  return m;
}

but this does:

Man goo(){
    return Man();
}
Delimitry
  • 2,987
  • 4
  • 30
  • 39
ozma
  • 1,633
  • 1
  • 20
  • 28

3 Answers3

4
Man m();

This means "somewhere else in the program, I will define a function named m that takes no arguments and returns a Man". Yes, even when you write it inside another function.

Man m;

This means "m is a variable of type Man". Since Man is a class type, the default constructor will be called and no parentheses are necessary.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
3

You don't want those parentheses in your first example:

Man goo(){
  Man m;
  return m;
}
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
2

You don't need () in the first case. The default constructor is called implicitly.

Man goo(){
    Man m;
    return m; 
}

In the second case you are calling the constructor.

user1192525
  • 657
  • 4
  • 20