root-project / cling

The cling C++ interpreter
Other
3.53k stars 269 forks source link

function definition is not allowed here #184

Open gouarin opened 7 years ago

gouarin commented 7 years ago

This following example doesn't work in cling (tested with cling 3.0 and cling 0.5-dev)

****************** CLING ******************
* Type C++ code and press enter to run it *
*             Type .q to exit             *
*******************************************
[cling]$ #include <iostream>
[cling]$ struct foo
[cling]$ {
[cling]$ ?       double x;
[cling]$ ?   };
[cling]$ std::ostream& operator<<(std::ostream &output, foo f){
[cling]$ ?       output << f.x;
[cling]$ ?       return output;
[cling]$ ?   }
input_line_6:2:55: error: function definition is not allowed here
 std::ostream& operator<<(std::ostream &output, foo f){
chrgod commented 6 years ago

Using cling via xeus-cling in a juptyer notebook, the following workaround did it for me:

#define INSERTION_OPERATOR operator<< // Workaround for a bug of cling

std::ostream & INSERTION_OPERATOR( std::ostream & os, const Vector2 & v )
{
    os << "(" << v.x() << ", " << v.y() << ")";
    return os;
}

#undef INSERTION_OPERATOR 
martinRenou commented 5 years ago

Putting the operator implementation inside a namespace also seems to do the trick

namespace test
{
    std::ostream & operator<<( std::ostream & os, const Vector2 & v )
    {
        os << "(" << v.x() << ", " << v.y() << ")";
        return os;
    }
}
slyalin commented 1 year ago

Just experienced the same issue when using in jupyter xeus-cling notebook, error still exists. Any plans to fix it?