Question

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!

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top