0

I created a simple class to pass to the sort method of a Juce Array http://www.rawmaterialsoftware.com/api/classArray.html#ac1dca4ab2895315dd85e25eaca2fcab1

It looks like this:

class XComparison
{
public:
    static int compareElements (StraightPath first, StraightPath second)
    {
        return (int) (first.xOrigin - second.xOrigin);
    }
};

When I create an instance of this to pass to the comparator, these two work:

XComparison x;
XComparison x = XComparison();

but this one gives me a compiler error:

XComparison x();

"left of '.compareElements' must have class/struct/union" on lines 74, 101, 119 of http://juce.git.sourceforge.net/git/gitweb.cgi?p=juce/juce;a=blob;f=modules/juce_core/containers/juce_ElementComparator.h;h=f976c40c7741b3df30d10e699c282a3569a49e3c;hb=HEAD#l74

Why doesn't the implicit assignment work here?

Thanks!

1
  • 2
    This has 99999 duplicates of people running into this hilarity. Commented Apr 26, 2012 at 23:12

2 Answers 2

4

XComparison x(); is parsed as a function declaration of x, which takes no arguments and returns an XComparison.

http://yosefk.com/c++fqa/ctors.html#fqa-10.19

Sign up to request clarification or add additional context in comments.

Comments

1

You're declaring a function not an expression that is why you get the error.

XComparison x; // default construction, unitialised
XComparison x = XComparison(); // construct x from default constructor
XComparison x(); // all you are doing is declaring a function x that returns XComparison

See this C++ faq

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.