问题
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