为什么我会收到这个'redeclared as different kind of symbol'错误?



>我有一个这样的函子,

class PrintParentheses
{
public:
    PrintParentheses(unsigned pairsCount)
    {}
    void operator ()() {}
};

在里面main()我使用它就像,

#include <iostream>
int main()
{
  unsigned pairsCount = 0;
  // Error:  ‘PrintParentheses pairsCount()’ redeclared as different kind of symbol
  PrintParentheses(pairsCount)();
  PrintParentheses(5)(); // But this works
}

错误位置在代码本身内标记。我已经测试了GCC-4.6clang-3.1.两者都给出相同的错误。

这被解读为 pairsCount 是一个不带参数并返回 PrintParenthes 的函数。由于所谓的最烦人的解析,必须将其视为函数声明。相反,创建一个对象并使用它:

PrintParentheses obj(pairsCount);
obj();

最新更新