Why am I getting this 'redeclared as different kind of symbol' error?

可紊 提交于 2019-12-23 23:15:05

问题


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.


回答1:


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();


来源:https://stackoverflow.com/questions/12776022/why-am-i-getting-this-redeclared-as-different-kind-of-symbol-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!