【问题标题】:Pass pointer to function works in simple case but not after "class"-ifying将指针传递给函数在简单情况下有效,但在“类”化后无效
【发布时间】:2017-07-22 23:21:26
【问题描述】:

我是一个有点生疏的程序员,并且是 C++ 新手。我被要求编写一个程序,该程序可以将一个函数的指针传递给另一个函数并执行。我可以使简单的案例工作,其中所有内容都在 .cpp 文件中。但是当我将代码放在 .h 文件中的类中时,它不会编译。我要么是代码盲,要么是遗漏了什么。

这是有效的代码:

/*
 *  funcptr.cpp
 *
 *  Example:
 *  - pass function pointer as argument
 *  - execute passed function
 */
#include <stdio.h>

void takes_a_function(void (*f)(void *data), void *data);
void print_char(void *data);
void print_int(void *data);

// This function gets passed (to takes_a_function)
void print_char(void *data) {
  char *ch = (char *)data;
  printf("%c\n", *ch);
}

// This function gets passed (to takes_a_function)
void print_int(void *data) {
  int *i = (int *)data;
  printf("%d\n", *i);
}

void takes_a_function(void (*f)(void *), void *data) {
    //f(data);    // this also works
    (*f)(data);
}

int main() {

  int i = 100;
  takes_a_function(print_int, &i);

  char ch = 'A';
  takes_a_function(print_char, &ch);

}

编译并运行:

# g++ funcptr.cpp -o funcptr
# ./funcptr
100
A

到目前为止一切顺利。但随后我将代码放入一个 .h 文件并对其进行“类”化,以便我可以在任何地方使用它,然后一切都崩溃了:

#ifndef __funcptr_h__
#define __funcptr_h__

#include <stdio.h>

void takes_a_function(void (*f)(void *data), void *data);
void print_char(void *data);
void print_int(void *data);
void testit();

class FunctionPtr
{
    public:

    // This function gets passed (to takes_a_function)
    void print_char(void *data) {
        char *ch = (char *)data;
        printf("%c\n", *ch);
    }

    // This function gets passed (to takes_a_function)
    void print_int(void *data) {
        int *i = (int *)data;
        printf("%d\n", *i);
    }

    void takes_a_function(void (*f)(void *a), void *data) {
          //f(data);        // this also works
          (*f)(data);
    }

    void testit() {
        int i = 100;
        takes_a_function(print_int, &i);

        char ch = 'A';
        takes_a_function(print_char, &ch);
    }

};

#endif

编译错误是:

# g++ funcptr.h
funcptr.h: In member function ‘void FunctionPtr::testit()’:
funcptr.h:34:33: error: invalid use of non-static member function ‘void FunctionPtr::print_int(void*)’
   takes_a_function(print_int, &i);
                                 ^
funcptr.h:22:7: note: declared here
  void print_int(void *data) {
       ^~~~~~~~~
funcptr.h:37:35: error: invalid use of non-static member function ‘void FunctionPtr::print_char(void*)’
   takes_a_function(print_char, &ch);
                                   ^
funcptr.h:16:7: note: declared here
  void print_char(void *data) {
   ^~~~~~~~~~

我玩这个已经有一段时间了,并且在传递函数指针方面做了很多阅读,包括在 StackOverflow 上,但是我看到的所有示例都很简单并且没有任何帮助。

非常感谢任何见解。 谢谢大家。

【问题讨论】:

标签: c++ function-pointers


【解决方案1】:

在我看来,您的问题是当您使用参数调用 take_a_function 时。 您将 data 参数声明为指向 void 类型的指针,而不是对 void 类型的引用。 您可以尝试以下方法:

void testIt() {
    int i = 100;
    int * j = &i;
    takes_a_function(print_int, j);
    char c = 'a';
    char * d = &c;
    takes_a_function(print_char, d);
};

引用和指针并不完全相同。

在定义 __funtptr_h__ 之后,您似乎忘记了 #endif

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-09
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    • 1970-01-01
    相关资源
    最近更新 更多