简体   繁体   中英

int Func(function <int(int)> F, int x)

I've got a code from the class, i can't get it to work on visual studio 2015, what is the problem, and can someone help me understand this line: int Func(function F, int x) ?

int Func(function <int(int)> F, int x)
{
    return F(x)*F(x) + 1;
}
int G(int x) 
{ 
    return x + 1;
}
int main(int argc, const char * argv[])
{
    cout << "Func(G, 5) = " << Func(G, 5) << endl;
}`

why the code isn't running ?

Make sure you are including the functional header and using the correct namespaces.

https://en.wikipedia.org/wiki/Function_pointer#In_C++

#include <iostream>
#include <functional>

int Func(const std::function<int(int)> F, int x)
{
    return F(x)*F(x) + 1;
}

int G(int x) 
{ 
    return x + 1;
}

int main(int argc, const char * argv[])
{
    std::cout << "Func(G, 5) = " << Func(G, 5) << std::endl;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM