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,342
  • 14
  • 89
  • 184

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
  • 245,575
  • 61
  • 446
  • 609
2

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

Maksim Skurydzin
  • 9,841
  • 8
  • 38
  • 52