2

I have a functor like this,

class PrintParentheses
{
public:
    PrintParentheses(unsigned pairsCount)
    {}

    void operator ()() {}
};

Inside main() I am using it like,

#include <iostream>

int main()
{
  unsigned pairsCount = 0;

  // Error:  ‘PrintParentheses pairsCount()’ redeclared as different kind of symbol
  PrintParentheses(pairsCount)();

  PrintParentheses(5)(); // But this works

}

Error positions are marked inside the code itself. I have tested both GCC-4.6 and clang-3.1. Both are giving the same error.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
Hindol
  • 2,924
  • 2
  • 28
  • 41
  • 1
    `PrintParentheses(pairsCount)();` declares `pairsCount` as a function returning a `PrintParentheses`. I know this has been asked several times before, but I'm having trouble finding it. –  Oct 08 '12 at 05:57
  • @hvd, Haven't seen it asked quite like this. The syntax is different than normal. I have to say the added parentheses made an impression on me at first. – chris Oct 08 '12 at 05:57
  • @chris Perhaps you're right, the more commonly seen syntax that causes confusion is `T1 x(T2())`, not `T1(x)()`, even though the underlying problem is the same. –  Oct 08 '12 at 06:00
  • @hvd, If there's one thing I've learned from SO, it's that the same question can be asked many different ways. One I see a lot is `Class obj();` when trying to explicitly call the default constructor. – chris Oct 08 '12 at 06:03
  • 1
    I took the liberty to simplify the code. In general, you should try to provide short code samples from which one can easily reproduce the problem. A lot of the code in your original was irrelevant. – juanchopanza Oct 08 '12 at 06:09
  • @juanchopanza Thank you for that. I simplified my code but did not know some portions were irrelevant _before seeing the answer_. – Hindol Oct 08 '12 at 06:11
  • @Hindol, True, you did make an attempt, which is great, but always try to cut down as much as you can for the optimal chance at solving it without even having to ask, or giving people, when you do ask, a very obvious sample that requires very little thought to spot the error in if one is capable of spotting it. – chris Oct 08 '12 at 06:20

1 Answers1

8

That's being read as pairsCount is a function taking no arguments and returning PrintParentheses. Due to what is known as the Most Vexing Parse, this must be treated as a function declaration. Instead, create an object and use it:

PrintParentheses obj(pairsCount);
obj();
chris
  • 60,560
  • 13
  • 143
  • 205