在 C++ 类的另一个头文件中定义的调用函数
Calling function defined in another header file in a class in C++
我正在尝试在一个类中使用带有shiftreg.cpp的shiftreg.h(我之前在我的主文件中成功使用过)并调用他的一个函数。可悲的是,is没有编译并给出错误:'shiftWrite'没有在这个范围内声明。我想我将每个头文件都包含在其他每个头文件中,它们应该找到彼此。出了什么问题?
这是 shiftreg.h:
#ifndef shiftreg_h
#define shiftreg_h
void shiftWrite(uint8_t data);
#endif
而这个班次.cpp:
#include "ShiftReg.h"
void shiftWrite(uint8_t data) {
do something;}
函数shiftWrite,我想在类SegmentDisplay的函数中使用如下,与SegmentDisplay.h
#ifndef SegmentDisplay_h
#define SegmentDisplay_h
#include <ShiftReg/shiftReg.h>
class SegmentDisplay{
public:
void pickNumber(int x);
};
#endif
和片段播放.cpp:
#include "SegmentDisplay.h"
void SegmentDisplay::pickNumber(int x){
shiftWrite(ONE);}
这一切都始于Arduino软件:
#include "SegShift.h"
SegmentDisplay dsp();
int main(void){
while(1){
dsp.pickNumber(j);}}
您似乎包含两个不同的文件:
在 ShiftReg 中.cpp您可以执行以下操作: #include "ShiftReg.h"
在 SegmentDisplay.h 中,您可以执行#include <ShiftReg/shiftReg.h>
(顺便说一下,我只是在你的SegmentDisplay.cpp中包含shiftreg.h。
我已将 ShiftReg.h 和 ShiftReg.cpp 文件添加到 SegmentDisplay 文件的文件夹中,然后它就可以编译了!我很高兴我也在你的答案的帮助下解决了这个问题。
但是,我想知道为什么当文件位于不同的文件夹中时,我无法使用这些文件。有什么想法吗?
在 SegmentDisplay.h 中,您必须包含 shiftreg.cpp而不是 shiftreg.h
否则,代码看不到声明的函数
相关文章:
- 使用用户定义的参数调用future/async并调用类方法
- 从python调用openMP共享库时,未定义opnMP函数
- 尝试调用 .h 文件中定义的变量时出现变量未定义错误
- 为用户定义的类正确调用复制构造函数/赋值运算符
- 如何知道链自定义算子<<调用次数?
- 自定义 std::fstream,std::filebuf 的溢出和下溢函数未为每个字符调用
- C++调用和定义不匹配
- 在 Microsoft Access SQL 中调用自定义 DLL 函数时传递的内存地址无效
- Tbb 库:错误:编写自定义类函数而不是 lambda 表达式时,对函数的调用不匹配
- clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) - 体系结构的未定义符号 x86_64:
- 调用 Win32 API 函数时未定义的引用
- Linux c++.在预加载的共享库中定义的基类的崩溃调用函数
- C++:对函子重载调用运算符的未定义引用
- 在销毁期间从另一个线程调用对象上调用方法是否未定义行为?
- c++ 中的自定义分配器,用于不调用secure_string实现
- 如何生成C++定义调用的动态生成
- 根据给定的类型定义调用 std::to_string 或 std::to_wstring
- 制作对inner_product(C++STL)的自定义调用
- c++自定义调用约定
- 已经或将要使用C++14或C++1z使其不再未定义调用委托类成员函数指针