xeus-cling
xeus-cling copied to clipboard
[cling] Function definition is not allowed here
Where declaring a variable before defining a function in cell, we get:
However, declaring the variable after the function or separating the two statement in two cells works fine.
If you invert the order it works. So, I am not sure that it is a xeus-cling issue...
A variant: a single cell containing two functions:
int f(int i) {
return 2;
}
int g(int i) {
return 3;
}
triggers an error:
input_line_7:5:15: error: function definition is not allowed here
int g(int i) {
^
Other variant:
using namespace std;
int f(int i) {
return 1;
}
This has been solved upstream and will be resolved when we update to the latest version of cling.
This has been solved upstream and will be resolved when we update to the latest version of cling.
Great!
For the record, another instance:
vector<int> f() {
return {};
}
triggers also a function definition not allowed here
. Alas we can't update easily our installation, and this is annoying for the coming week which is about functions and vectors. Would you by any chance have a workaround?
One thing that work is to use a
typedef vector<int> tableau
but this adds another thing to explain to our students.
Thanks in advance,
@nthiery this issue has been fixed in the master branch of cling https://github.com/root-project/cling/issues/182
We hope that we could have a newest version of cling for xeus-cling soon.
A better workaround is to use auto
auto f()
{
return std::vector<int>{};
}
So you have to follow the rule of AAA (Almost Always Auto) ;)
On Sun, Oct 08, 2017 at 02:35:28PM -0700, Loic Gouarin wrote:
[1]@nthiery this issue has been fixed in the master branch of cling [2]root-project/cling#182
We hope that we can have a newest version of cling for xeus-cling soon.
Yup.
A better workaround is to use auto auto f() { return std::vector
{}; } So you have to follow the rule of AAA (Almost Always Auto) ;)
Ah right, of course.
That's indeed the solution I would want to teach them in the longer run. However they only have 3 weeks of programming in their fingers, so I had implicitly crossed out the use of auto in my mind. Now I need to think whether I want to keep crossing it out or not. That's one more concept to introduce and we chose to use C++ rather than Python to be more explicit on static types.
Hmm, pondering, pondering, ...
Thanks Loïc!
Shoot, auto
as return type is C++14, and we are for now just using C++ 11. Ok, maybe we should switch, but not tomorrow. I'll use a typedef for now ...
Does anyone know why this causes an error: function definition is not allowed here
:
std::tuple<std::string, int> testTuple()
{
std::string s = "string";
int i = 0;
return std::make_pair(s, i);
}
@momonala this is due to the presence of <
, >
characters in the return type. If you use auto
, your problem is gone ;).
It is fixed in the cling 0.5 version.
.rawInput funtion()
This is still failing with cling 0.5:
vector<int> f() {
return vector<int>
}
input_line_9:2:18: error: function definition is not allowed here
vector<int> f() {
^
(info-111) [~] conda list | grep cling clangdev 5.0.0 cling_1 [cling] QuantStack
cling 0.5 7 [cling] QuantStack
cling-patches 1 0 conda-forge
cryptopp 5.6.5 cling_0 [cling] QuantStack
llvmdev 5.0.0 cling_1 [cling] QuantStack
pyzmq 17.0.0 cling_0 [cling] QuantStack
xeus 0.14.1 cling_0 [cling] QuantStack
xeus-cling 0.4.7 0 QuantStack
zeromq 4.2.5 cling_0 [cling] QuantStack
Yet another case: instantiation of a friend function (with a Xeus-cling installed by Conda and updated this week):
class Vector3
{
public :
Vector3(double x, double y, double z);
Vector3() = default;
friend Vector3 operator+(const Vector3& v1, const Vector3& v2);
private :
double x_ = 0.;
double y_ = 0.;
double z_ = 0.;
};
// In another cell:
Vector3 operator+(const Vector3& v1, const Vector3& v2)
{
Vector3 ret;
ret.x_ = v1.x_ + v2.x_;
ret.y_ = v1.y_ + v2.y_;
ret.z_ = v1.z_ + v2.z_;
return ret;
}
Hi!
The examples are still failing. Any hope for progress? Why has this ticket been closed?
I second this thread, it's still an issue.
This fails (Interpreter Error: )
vector<vector<int>> SimpleTest() {
vector<vector<int>> v {{1, 2, 3}, {4, 5, 6}};
return v;
}
input_line_44:2:35: error: function definition is not allowed here
vector<vector<int>> SimpleTest() {
This runs just fine:
auto SimpleTest() {
vector<vector<int>> v {{1, 2, 3}, {4, 5, 6}};
return v;
}
This runs only with std::vector
, not with vector
though:
std::vector<int> OneDimensionalTest() {
std::vector<int> v {1, 2, 3};
return v;
}
Any reason for not reopening this ticket?
Do you have the error with cling? If so , this is a bug to report upstream, otherwise we can reopen this issue.
Salut Johan!
I just tried, and did not manage:
cling
****************** CLING ******************
* Type C++ code and press enter to run it *
* Type .q to exit *
*******************************************
[cling]$ auto i=3;
[cling]$ void foo() {}
But I am not sure whether this is relevant: it's indeed not clear to me how to reproduce this in plain cling: here how to group the two lines so that they are evaluated at once, like when they are in the same cell.
This one does fail though with cling:
[cling]$ #include <vector>
[cling]$ using namespace std;
[cling]$ vector<int> f() { return vector<int>(); }
input_line_8:2:18: error: function definition is not allowed here
vector<int> f() { return vector<int>(); }
So at least this piece is for upstream.
@nthiery you can define a multiline function in cling, just hit enter after you open the curly brace. You will see a question mark on the new line (and on each new line until you close the curly brace).
@nthiery you can define a multiline function in cling, just hit enter after you open the curly brace. You will see a question mark on the new line (and on each new line until you close the curly brace).
Indeed; I just made it a one-liner for conciseness.
What I don't know how to do (and if it even makes sense) is to put two functions at once. As if they were in the same cell.
Oh, but now that you make me think about it; this ought to be valid code, but it's not:
[cling]$ void f() {} void g() {}
input_line_4:2:11: error: function definition is not allowed here
So in summary: several thing in a single cell is behaving like several thing in a single input line for cling. And the latter has limitations.
Not to beat a dead horse, but yet another example:
using coord_ext = std::array<float, 6>;
using surf_normal = std::array<float, 3>;
using face_data = std::tuple<coord_ext, coord_ext, coord_ext, coord_ext, surf_normal>;
auto generate_quad_strip(std::vector<coord_ext> rib1, std::vector<coord_ext> rib2)
{
vector<face_data> quad_strip;
auto rib1_iter = std::begin(rib1),
rib2_iter = std::begin(rib2),
rib1_end = std::end(rib1),
rib2_end = std::end(rib2);
/*
[snip]
*/
return quad_strip;
}
I don't think this really counts as closed.
Are we reopening this ticket since this issue still seems to persist in latest cling release.
EDIT: removing static did the trick for me.
#pragma cling add_include_path(<path_to_eigen>)
#include <Eigen/Dense>
#include <vector>
static Eigen::VectorXd returnARandomVector(){
Eigen::VectorXd vect;
vect << 1,3,4,5;
return vect;
}
static std::vector<std::uint64_t> foo(){
std::vector<std::uint64_t> vec_int;
return vec_int;
}
Hi!
Is it failing for any function now or am I doing something wrong?
#include <iostream>
using namespace std;
void f() {
cout << "Hi" << endl;
}
The kernel complains about f()
. And every modification I tried failed...
Hi @lucasturci. Have you tried putting the function definition just by itself in a separate cell? IIRC that is still needed,
Hi @lucasturci. Have you tried putting the function definition just by itself in a separate cell? IIRC that is still needed,
Yeah, it worked, thanks!
So... What does xeus-cling remember across cells and what does it forget?
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello jupyter"<<endl;
}
input_line_16:5:1: error: function definition is not allowed here { ^
I used include