8

I have a class that does not have a default constructor, I created a variable without giving parameters by mistake, but instead of a nice compiler error, I got a linker error, where I couldn't find the line of code that was causing it.

In the end, I managed to find the code that caused this, and only then I noticed that I was getting this warning:

C++: warning: C4930: prototyped function not called (was a variable definition intended?)

What's weird is when I changed the code from:

MyClass foo();

to

MyClass foo;

I did get a compiler error.

Can someone explain to me why the compiler suddenly started acting strange, is it a bug or something?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
sashoalm
  • 75,001
  • 122
  • 434
  • 781

1 Answers1

13

This

MyClass foo();

is a function declaration that has return type MyClass and does not accept arguments..

This

MyClass foo;

is an object definition. As your class MyClass has no the default constructor the compiler issues an error.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • "does not accept argument" is not true. `MyClass foo();` accepts an infinite number of arguments. See http://stackoverflow.com/a/47693/1767861 – Thomas Ruiz Nov 28 '13 at 11:20
  • 10
    You are wrong. The function does not accept arguments. it is not C. it is C++. So the meaning of empty parentheses is different in C and C++. – Vlad from Moscow Nov 28 '13 at 11:22