c++ - 将 C 转换为 C++ : typdef generating error

标签 c++ c typedef

我需要将 .c 文件转换为 .cpp 文件,我遇到了这个声明:

 typedef void handler_t(int);

 handler_t *Signal(int signum, handler_t *handler);

我在头文件中包含了这两行代码,并在 .cpp 文件中添加了实际的函数声明函数。

 handler_t *Signal(int signum, handler_t *handler){ ...... } 

当我这样做时,我收到错误:“handler_t does not name a type”。我以前从未在 C 或 C++ 中使用过 typdef,所以有人可以向我解释为什么我收到错误吗?


我按要求上课:

 #ifndef A_H
 #define A_H

 class A
 {
     public:
       A();
       virtual ~A();

       typedef void handler_t(int);
       handler_t* Signal(int signum, handler_t *handler);

     protected:
     private:
 };

   #endif // A_H

/////////////

   #include "A.h"

   A::A()
   {
   }

   A::~A()
   {
   }

   handler_t* A::Signal(int signum, handler_t *handler) {

     ...........

    }

错误:

    |694|error: ‘handler_t’ does not name a type|

最佳答案

尝试将其更改为:

typedef void (*handler_t)(int);

和:

handler_t Signal(int signum, handler_t handler);

引用查看signal()它是这样的:

#include <signal.h>

typedef void (*sighandler_t)(int);

sighandler_t signal(int signum, sighandler_t handler);

实际上,看看您的新代码,我认为您必须这样做:

A::handler_t* A::Signal(int signum, A::handler_t *handler)

我认为您的新错误“对 main 的 undefined reference ”与提出的问题无关。参见 this post一些想法。

关于c++ - 将 C 转换为 C++ : typdef generating error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15456240/

相关文章:

c++ - 使用 C++ 模板编译类成员变量?

c++ - C++ 类是否可以在头文件中包含静态常量 std::array 初始化内联?

c - 将文件解析为C中的结构

c - typedef 结构错误 C2275 错误

c# - 将带有参数的 c# 回调方法传递给 c++ dll 会导致 System.ExecutionEngineException

c++ - "separately compiled C++ templates"是什么?

c - C中的赋值和条件检查

c - C代码的基本错误

c - 这些 struct 和 typedef 定义是什么意思?

iphone - typedef 枚举 : which classes have access to it