1

This is my code. Every time I try to compile it, it gives me this error
expected primary-expression before 'float' at line 3

#include<iostream>
using namespace std;
auto fun = (float x){
    return 1/(1-x);
};
int main(){
    auto x=fun(0.5);
    cout<<x;
    return 0;
}
2
  • 3
    Looks like you have got lambdas and functions mixed up. If you just remove that = it should work - as long as you have C++14 enabled. Commented Jul 24, 2020 at 11:05
  • 2
    While there is an answer to your question (see answers below), what problem are you trying to solve that isn't better solved by the idiomatic way declaring a function using a function declaration? Commented Jul 24, 2020 at 11:17

2 Answers 2

1

With the assignment operator (i.e., =) I can think of a lambda instead of a function:

auto fun = [](float x){
    return 1/(1-x);
};

That is, just by adding empty square brackets (i.e., [], which corresponds to the lambda capture list) before the parameter list, turns the code following the = into a lambda expression.

Technically, the = above isn't really the assignment operator. It just belongs to the syntax of copy initialization.

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

Comments

0
auto fun = [](float x){
    return 1/(1-x);
};

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.