Unable to find appropriate '=' operator


#1

Consider the code below — MakeController is a static function injected into the scripting system. I am having a problem where I’m getting an exception on the second assignment to the ‘cc’ variable, i.e, the line
cc = MakeController(1,v);

The reason is "Unable to find appropriate ‘=’ operator but I have absolutely no idea what that means.

Any insights would be appreciated.

 def OnActivate()
    {
      var v = Round( K1.GetValue() * 127);
      var cc = MakeController(71, v);
      Twister.SendMidiMessage(cc);

      v = Round (K2.GetValue() * 127) ;
      cc = MakeController(1, v);
      // Twister.SendMidiMessage(cc);
    }

#2

That means that you need to supply an operator= for whatever type is returned from MakeController.

It would appear that what you really want to do is something like:

def OnActivate()
{
  {
    var v = Round( K1.GetValue() * 127);
    var cc = MakeController(71, v);
    Twister.SendMidiMessage(cc);
  }

  {
    var v = Round (K2.GetValue() * 127) ;
    var cc = MakeController(1, v);
    Twister.SendMidiMessage(cc);
  }
}

which avoids the reassignment entirely.

-Jason


#3

Thanks for the quick response. I guess I need to inject operator= into the scripting system. I don’t want users to have to create separate blocks for this. What is the syntax for injecting an operator into a script, is it exactly the same as injecting a function name?

Frankly, it never occurred to me that this was the problem. Is there not a default assignment operator that would just do a shallow copy (which in fact is all that’s needed for this particular class)?


#4

Never mind - I found your example

chai.add(fun(&MyClass::operator+), “+”);


#5

Actually, that doesn’t work.
So there’s a definition in my class called

 MidiMessage& operator= (const MidiMessage& other);

but the line
chai.add(chaiscript::fun(&MidiMessage::operator=), “=”);
doesn’t compile, I get a message no matching function for call to ‘fun’

What am I missing (other than having forgotten too much C++)


#6

Do you have multiple overloads for operator= ? Can you paste the full error you are getting?

-Jason


#7

No overloads, just the one. But the issue I’m having is that I don’t know how to inject an operator into my chaiscript from C++. I can’t figure out the right syntax for the chai.add(fun…) that compiles for an operator example


#8

Right, and what I’m trying to say is that I need more info, because what you showed looks correct and matches other use inside the ChaiScript code base.


#9

Happy to give you any info you like but not sure what you need. There are no multiple overloads for operator=, just the one defined in the MidiMessage class.

The compile error for
> chai.add(chaiscript::fun(&MidiMessage::operator=)), “=”);

is “No matching function for call to ‘fun’”

I’m not sure whether it’s relevant but the method inside of which this code is written is not part of the class MidiMessage.


#10

There has to be more to the compile time error than that, probably pages of different overloads that it attempted. I need either a reproducible example or those pages of errors.


#11

I’m sorry — I’m so used to the little red text in the IDE giving me enough information that I completely forgot there are more details to be found in logging.

/Volumes/HD2/src/Deskew/Projects/TheGigRack/Source/Processors/MIDIMessageReceiver.cpp:1282:44: No matching function for call to ‘fun’
/Volumes/HD2/src/Deskew/Projects/TheGigRack/ChaiScript/chaiscript/dispatchkit/register_function.hpp:39:20: Candidate template ignored: couldn’t infer template argument ‘T’
/Volumes/HD2/src/Deskew/Projects/TheGigRack/ChaiScript/chaiscript/dispatchkit/register_function.hpp:48:20: Candidate template ignored: couldn’t infer template argument ‘Ret’
/Volumes/HD2/src/Deskew/Projects/TheGigRack/ChaiScript/chaiscript/dispatchkit/register_function.hpp:58:20: Candidate template ignored: couldn’t infer template argument ‘Ret’
/Volumes/HD2/src/Deskew/Projects/TheGigRack/ChaiScript/chaiscript/dispatchkit/register_function.hpp:67:20: Candidate template ignored: couldn’t infer template argument ‘Ret’
/Volumes/HD2/src/Deskew/Projects/TheGigRack/ChaiScript/chaiscript/dispatchkit/register_function.hpp:78:20: Candidate template ignored: couldn’t infer template argument ‘T’
/Volumes/HD2/src/Deskew/Projects/TheGigRack/ChaiScript/chaiscript/dispatchkit/register_function.hpp:105:20: Candidate function template not viable: requires 2 arguments, but 1 was provided
/Volumes/HD2/src/Deskew/Projects/TheGigRack/ChaiScript/chaiscript/dispatchkit/register_function.hpp:131:20: Candidate function template not viable: requires 3 arguments, but 1 was provided


#12

Ok, something weird is definitely going on. Let’s try this, using a lambda, which should get around any possible weirdness and work regardless of how your operator= has come into existence.

chai.add(
  chaiscript::fun(
    [](MidiMessage &lhs, const MidiMessage &rhs) -> MidiMessage & {
      return lhs = rhs;
    }
  )
  , "=");


#13

Thanks for the help — this lambda version compiles fine – I have to put the script back again to test it. However, I understand how it works though — good solution.


#14

For bonus points, the more generic version would be something like:

chai.add(
  chaiscript::fun(
    [](MidiMessage &lhs, const MidiMessage &rhs) -> decltype(auto) {
      return (lhs = rhs);
    }
  )
  , "=");

But that’s probably not necessary for what you’re doing. this syntax allows the lambda to deduce that the return type is a & instead of a value.

-Jason