C++ 无法使用const struct timepec重写纯虚拟函数*

C++ 无法使用const struct timepec重写纯虚拟函数*,c++,inheritance,virtual,pure-virtual,C++,Inheritance,Virtual,Pure Virtual,下面是我想要实现的纯虚拟接口类: #include <time.h> class SharedMemoryInterface { public: virtual ~SharedMemoryInterface() {} virtual int sem_timedwait(sem_t* sem, const struct timepsec* abs_timeout) = 0; }; 我发现编译器错误: SharedMemoryImpl.h:25:7: note:

下面是我想要实现的纯虚拟接口类:

#include <time.h>
class SharedMemoryInterface
{
public:
    virtual ~SharedMemoryInterface() {}
    virtual int sem_timedwait(sem_t* sem, const struct timepsec* abs_timeout) = 0; 
};
我发现编译器错误:

SharedMemoryImpl.h:25:7: note:   because the following virtual functions are pure within "SharedMemoryImpl":
 class SharedMemoryImpl : public SharedMemoryInterface
SharedMemoryInterface.h:27:17: note:    virtual int SharedMemoryInterface::sem_timedwait(sem_t*, const timepsec*)
     virtual int sem_timedwait(sem_t* sem, const struct timepsec* abs_timeout) = 0;

唯一的区别似乎是timespec参数,它删除了结构,原型不再匹配,为什么要这样做?

您在
SharedMemoryInterface::sem_timedwait
中有一个输入错误:您编写了
timepsec
,而不是
timespec

通常这会导致错误,但您使用了
struct
关键字。当编译器看到
struct timepsec
时,它要么找到名为
timepsec
的结构(忽略任何同名函数),要么forward声明一个新结构(如果找不到)。因此,
struct
的使用掩盖了打字错误。当您在
SharedMemoryImpl
中正确拼写
timespec
时,它当然指的是不同的类型。因此,
SharedMemoryInterface
中的纯虚拟函数不会被覆盖


顺便说一句,没有编译器警告捕获这些拼写错误的转发声明。在C++中,我建议简单地避免使用精细类型说明符是一个好的做法,除非你真的需要在C和C++中编译代码(很明显,这里不是这样),或者你需要引用一个与函数相同名称的结构/类(很明显,这样命名是不好的,但是C库有时会这样做)。.

您在
SharedMemoryInterface::sem_timedwait
中有一个输入错误:您编写了
timepsec
而不是
timespec

通常这会导致错误,但您使用了
struct
关键字。当编译器看到
struct timepsec
时,它要么找到名为
timepsec
的结构(忽略任何同名函数),要么forward声明一个新结构(如果找不到)。因此,
struct
的使用掩盖了打字错误。当您在
SharedMemoryImpl
中正确拼写
timespec
时,它当然指的是不同的类型。因此,
SharedMemoryInterface
中的纯虚拟函数不会被覆盖


顺便说一句,没有编译器警告捕获这些拼写错误的转发声明。在C++中,我建议简单地避免使用精细类型说明符是一个好的做法,除非你真的需要在C和C++中编译代码(很明显,这里不是这样),或者你需要引用一个与函数相同名称的结构/类(很明显,这样命名是不好的,但是C库有时会这样做)。.

注意
timepsec
timespec
的对比。修复您的输入错误。为什么在实现中需要额外的
}
?它编译了警告,也就是说,是时候回家了。注意
timepsec
timespec
。修复您的输入错误。为什么在实现中需要额外的
}
?是时候回家了
SharedMemoryImpl.h:25:7: note:   because the following virtual functions are pure within "SharedMemoryImpl":
 class SharedMemoryImpl : public SharedMemoryInterface
SharedMemoryInterface.h:27:17: note:    virtual int SharedMemoryInterface::sem_timedwait(sem_t*, const timepsec*)
     virtual int sem_timedwait(sem_t* sem, const struct timepsec* abs_timeout) = 0;