0

Possible Duplicate:
Error on calling default constructor with empty set of brackets
What's the differences between Test t; and Test t();? if Test is a class

What is the difference between this code the first compiles the second doesn't. I am pretty new to C++.

FIRST SAMPLE

list<string> str;
list<string>::iterator it;

it = str.begin();

SECOND SAMPLE

list<string> str();
list<string>::iterator it;

it = str.begin();

I thought that calling without the parantethiss calls the default constructor witch is the same to () variant.

Community
  • 1
  • 1
opc0de
  • 11,557
  • 14
  • 94
  • 187

2 Answers2

2
list<string> str;

declares a variable.

list<string> str();

declares a function that takes no parameters and returns a list<string>.

This is commonly known as a vexing parse.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
2

list<string> str(); is treated as a function prototype declaration. See "Most Vexing Parse".

Maksim Skurydzin
  • 10,301
  • 8
  • 40
  • 53