C++ 中的函数指针对象前向声明
我在 C++ 头文件中有一组函数指针对象,我将此文件包含在主头文件中,然后尝试在另一个 C++ 文件中使用这些对象(初始化函数指针,然后通过代码的另一部分使用这些指针)但是我总是收到“多重定义”错误。有没有办法在头文件中声明全局函数指针对象?
I have a group of function pointer objects in a header C++ file, I include this file in the main header file and then trying to use these objects in another C++ file (initialize function pointers and then use these pointers through another part of code) but I always get a "multiple defined" error. Is there a way how to declare global function pointer objects in a header file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在主标题中执行此操作。 (假设您使用的是名为 FnPointer 的 typedef)
然后在您的实现文件中
(
extern
表示该变量将存在,但我稍后将在其他编译单元中为其分配空间。)You can do this in your main header. (Assusming you're using a typedef called FnPointer)
Then in your implementation file
(
extern
means this variable will exist but I'm going to allocate space for it later in some other compilation unit. )就像使用任何其他全局指针一样使用它们。
extern
。第 1 步:
file.h
第 2 步:
file.cpp
第3步:
someOtherfile.cpp
Just use them like any other global pointers.
extern
in one of the header files.Step 1:
file.h
Step 2:
file.cpp
Step 3:
someOtherfile.cpp