如何将函数存储到变量中?

发布于 2024-10-30 17:10:18 字数 2053 浏览 4 评论 0原文

我认为它们被称为函子? (已经有一段时间了)

基本上,我想将指向函数的指针存储在变量中,这样我就可以从命令行指定要使用的函数。

所有函数都返回并采用相同的值。

unsigned int func_1 (unsigned int var1)
unsigned int func_2 (unsigned int var1)

function_pointer = either of the above?

那么我可以通过以下方式调用它: function_pointer(my_variable)?

编辑: 根据@larsmans的建议,我得到了这个: Config.h:

class Config
{
public:
    unsigned static int (*current_hash_function)(unsigned int);
};

Config.cpp:

#include "Config.h"
#include "hashes.h"
unsigned static int (*current_hash_function)(unsigned int) = kennys_hash_16;

hashes.h:

unsigned int kennys_hash(unsigned int out);
unsigned int kennys_hash_16(unsigned int out);

hashes.cpp:

just implements the functions in the header

main.cpp:

#include "Config.h"
#include "hashes.h"
// in test_network:
    unsigned int hashed = Config::current_hash_function(output_binary);

//in main():
        else if (strcmp(argv[i], "-kennys_hash_16") == 0)
        {
            Config::current_hash_function = kennys_hash_16;
        }
        else if (strcmp(argv[i], "-kennys_hash_8") == 0)
        {
            Config::current_hash_function = kennys_hash;
        }

我得到的错误:

g++ -o hPif src/main.o src/fann_utils.o src/hashes.o src/Config.o -lfann -L/usr/local/lib 
Undefined symbols:
  "Config::current_hash_function", referenced from:
      test_network()     in main.o // the place in the code I've selected to show
      auto_test_network_with_random_data(unsigned int, unsigned int, unsigned int)in main.o
      generate_data(unsigned int, unsigned int, unsigned int)in main.o
      _main in main.o // the place in the code I've selected to show
      _main in main.o // the place in the code I've selected to show
      generate_train_file()     in fann_utils.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [hPif] Error 1

I think they are called functors? (it's been a while)

Basically, I want to store a pointer to a function in a variable, so I can specify what function I want to use from the command line.

all the functions return and take the same values.

unsigned int func_1 (unsigned int var1)
unsigned int func_2 (unsigned int var1)

function_pointer = either of the above?

so then I could call it by going: function_pointer(my_variable)?

EDIT:
as per @larsmans's suggestion, I've gotten this:
Config.h:

class Config
{
public:
    unsigned static int (*current_hash_function)(unsigned int);
};

Config.cpp:

#include "Config.h"
#include "hashes.h"
unsigned static int (*current_hash_function)(unsigned int) = kennys_hash_16;

hashes.h:

unsigned int kennys_hash(unsigned int out);
unsigned int kennys_hash_16(unsigned int out);

hashes.cpp:

just implements the functions in the header

main.cpp:

#include "Config.h"
#include "hashes.h"
// in test_network:
    unsigned int hashed = Config::current_hash_function(output_binary);

//in main():
        else if (strcmp(argv[i], "-kennys_hash_16") == 0)
        {
            Config::current_hash_function = kennys_hash_16;
        }
        else if (strcmp(argv[i], "-kennys_hash_8") == 0)
        {
            Config::current_hash_function = kennys_hash;
        }

the error I get:

g++ -o hPif src/main.o src/fann_utils.o src/hashes.o src/Config.o -lfann -L/usr/local/lib 
Undefined symbols:
  "Config::current_hash_function", referenced from:
      test_network()     in main.o // the place in the code I've selected to show
      auto_test_network_with_random_data(unsigned int, unsigned int, unsigned int)in main.o
      generate_data(unsigned int, unsigned int, unsigned int)in main.o
      _main in main.o // the place in the code I've selected to show
      _main in main.o // the place in the code I've selected to show
      generate_train_file()     in fann_utils.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [hPif] Error 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

旧情别恋 2024-11-06 17:10:23

如果您安装了 Boost,您还可以查看Boost 函数

IF you have Boost installed, you can also check out Boost Function.

余厌 2024-11-06 17:10:22

你能做的最简单的事情是

unsigned int (*pFunc)(unsigned int) = func_1;

这是一个裸函数指针,它不能用来指向自由函数以外的任何东西。

如果您的编译器支持 C++0x auto 关键字,则可以减轻痛苦:

auto pFunc = func_1;

在任何情况下,您都可以使用以下命令调用该函数

unsigned int result = pFunc(100);

还有许多其他提供通用性的选项,例如:

  • 您可以使用boost::function 与任何 C++ 编译器
  • 使用实现 C++0x 功能的编译器,您可以使用 std::function

这些可用于指向任何可以使用适当的签名来调用(它实际上是实现称为函子的 operator() 的对象)。

更新(解决更新的问题)

您直接的问题是您尝试使用 Config::current_hash_function (您声明得很好)但无法定义它。

这定义了一个指向函数的全局静态指针,与 Config 类中的任何内容无关:

unsigned static int (*current_hash_function)(unsigned int) = kennys_hash_16;

这正是您所需要的:

unsigned int (*Config::current_hash_function)(unsigned int) = kennys_hash_16;

The simplest you can do is

unsigned int (*pFunc)(unsigned int) = func_1;

This is a bare function pointer, which cannot be used to point to anything other than a free function.

You can make it less painful if your compiler supports the C++0x auto keyword:

auto pFunc = func_1;

In any case, you can call the function with

unsigned int result = pFunc(100);

There are many other options that provide generality, for example:

  • You can use boost::function with any C++ compiler
  • With a compiler implementing features of C++0x you can use std::function

These can be used to point to any entity that can be invoked with the appropriate signature (it's actually objects that implement an operator() that are called functors).

Update (to address updated question)

Your immediate problem is that you attempt to use Config::current_hash_function (which you declare just fine) but fail to define it.

This defines a global static pointer to a function, unrelated to anything in class Config:

unsigned static int (*current_hash_function)(unsigned int) = kennys_hash_16;

This is what you need instead:

unsigned int (*Config::current_hash_function)(unsigned int) = kennys_hash_16;
对风讲故事 2024-11-06 17:10:22

从 C++11 开始,您可以使用 std::function 来存储函数。要存储函数,您可以将其用作follsonig:

std::function<返回类型参数类型)>

作为一个例子,它是:

#include <functional>
#include <iostream>

int fact (int a) {
    return a > 1 ? fact (a - 1) * n : 1;
}

int pow (int b, int p) {
    return p > 1 ? pow (b, p - 1) * b : b;
}

int main (void) {
    std::function<int(int)> factorial = fact;
    std::function<int(int, int)> power = pow;

    // usage
    factorial (5);
    power (2, 5);
}

From C++11 you can use std::function to store functions. To store function you use it as follsonig:

std::function<return type(parameter type(s))>

as an example here it is:

#include <functional>
#include <iostream>

int fact (int a) {
    return a > 1 ? fact (a - 1) * n : 1;
}

int pow (int b, int p) {
    return p > 1 ? pow (b, p - 1) * b : b;
}

int main (void) {
    std::function<int(int)> factorial = fact;
    std::function<int(int, int)> power = pow;

    // usage
    factorial (5);
    power (2, 5);
}
删除→记忆 2024-11-06 17:10:22

不,这些称为函数指针。

unsigned int (*fp)(unsigned int) = func_1;

No, these are called function pointers.

unsigned int (*fp)(unsigned int) = func_1;
走过海棠暮 2024-11-06 17:10:22

您还可以使用 c++0x 或 boost 中的函数。
那将是

boost::function<int(int)>

然后使用bind 将您的函数绑定到此类型。

看看这里这里

好的,这里是一个例子。我希望这有帮助。

int MyFunc1(int i)
{
    std::cout << "MyFunc1: " << i << std::endl;
    return i;
}

int MyFunc2(int i)
{
    std::cout << "MyFunc2: " << i << std::endl;
    return i;
}

int main(int /*argc*/, char** /*argv*/)
{
    typedef boost::function<int(int)> Function_t;

    Function_t myFunc1 = boost::bind(&MyFunc1, _1);
    Function_t myFunc2 = boost::bind(&MyFunc2, _1);

    myFunc1(5);
    myFunc2(6);
}

You could also use function either from the c++0x or from boost.
That would be

boost::function<int(int)>

and then use bind to bind your function to this type.

Have a look here and here

Ok here would be a example. I hope that helps.

int MyFunc1(int i)
{
    std::cout << "MyFunc1: " << i << std::endl;
    return i;
}

int MyFunc2(int i)
{
    std::cout << "MyFunc2: " << i << std::endl;
    return i;
}

int main(int /*argc*/, char** /*argv*/)
{
    typedef boost::function<int(int)> Function_t;

    Function_t myFunc1 = boost::bind(&MyFunc1, _1);
    Function_t myFunc2 = boost::bind(&MyFunc2, _1);

    myFunc1(5);
    myFunc2(6);
}
新一帅帅 2024-11-06 17:10:22

您可以通过这种方式将函数存储在 C++ 中的变量中

auto function_name = [&](params){
    statements 
};


auto add = [&](int a,int b){
    return a+b;
};
cout<<add(5,6);

You can store a function in a variable in c++ in this way

auto function_name = [&](params){
    statements 
};


auto add = [&](int a,int b){
    return a+b;
};
cout<<add(5,6);
烟花易冷人易散 2024-11-06 17:10:22
typedef unsigned int (*PGNSI)(unsigned int);

PGNSI variable1 = func_1;
PGNSI variable2 = func_2;
typedef unsigned int (*PGNSI)(unsigned int);

PGNSI variable1 = func_1;
PGNSI variable2 = func_2;
避讳 2024-11-06 17:10:22
unsigned int (* myFuncPointer)(unsigned int) = &func_1;

然而,函数指针的语法很糟糕,所以通常使用 typedef 它们:

typedef unsigned int (* myFuncPointerType)(unsigned int);
myFuncPointerType fp = &func_1;
unsigned int (* myFuncPointer)(unsigned int) = &func_1;

However, the syntax for function pointers is awful, so it's common to typedef them:

typedef unsigned int (* myFuncPointerType)(unsigned int);
myFuncPointerType fp = &func_1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文