Templates 试图在Arduino中使用模板而不是重载函数:类型未在此范围内声明
我正在尝试编写一个函数,它可以将数据移位到74HC595移位寄存器,该寄存器可以移位8、16和32位值 通过使用重载函数,我得到以下结果:Templates 试图在Arduino中使用模板而不是重载函数:类型未在此范围内声明,templates,arduino,shift-register,Templates,Arduino,Shift Register,我正在尝试编写一个函数,它可以将数据移位到74HC595移位寄存器,该寄存器可以移位8、16和32位值 通过使用重载函数,我得到以下结果: /********************************************************************************** * Software SPI Pin Settings ********************************************************************
/**********************************************************************************
* Software SPI Pin Settings
*********************************************************************************/
#define SPIPINPORT PORTB //The Port that the Pins are on.
#define LatchPin 2 //_RCLK Shift register clock pin
#define DataPin 3 //SER DS Serial data input
#define ClockPin 5
/**********************************************************************************
* Preproccesor PIN to PIN Mask
*********************************************************************************/
#define LATCHMASK (1 << LatchPin)
#define MOSIMASK (1 << DataPin)
#define CLOCKMASK (1 << ClockPin)
/**********************************************************************************
* Macros
*********************************************************************************/
#define tggl(port,bit) (port)^=(1<<(bit))
#define LATCH (SPIPINPORT &=~ LATCHMASK)
#define unLATCH (SPIPINPORT |= LATCHMASK)
#define PULSE { tggl(SPIPINPORT,ClockPin); tggl(SPIPINPORT,ClockPin); }
void zShiftClass::ShiftOut(uint8_t value)
{
LATCH;
for (uint8_t i = 0; i <= 7; i++)
{
if( !!(value&(1<<i)) == true) //If value is not a 1, turn off MOSIMASK
{ SPIPINPORT |= MOSIMASK; }
else
{ SPIPINPORT &= ~MOSIMASK; }
PULSE; //Pulse the Clock
}
unLATCH;
}
void zShiftClass::ShiftOut(uint16_t value)
{
LATCH;
for (uint8_t i = 0; i <= 15; i++)
{
if( !!(value&(1<<i)) == true) //If value is not a 1, turn off MOSIMASK
{ SPIPINPORT |= MOSIMASK; }
else
{ SPIPINPORT &= ~MOSIMASK; }
PULSE; //Pulse the Clock
}
unLATCH;
}
void zShiftClass::ShiftOut(uint32_t value)
{
LATCH;
for (uint8_t i = 0; i <= 31; i++)
{
if( !!(value&(1<<i)) == true) //If value is not a 1, turn off MOSIMASK
{ SPIPINPORT |= MOSIMASK; }
else
{ SPIPINPORT &= ~MOSIMASK; }
PULSE; //Pulse the Clock
}
unLATCH;
}
我做错了什么 好的,这属于“小心带礼物的开发工具”的范畴。Arduino草图工具是您的问题。如果在“首选项”菜单上打开“详细编译器输出”,您将对发生的情况有一些了解。用你的代码,我可以复制你的错误。编译名为template1的测试项目时,会报告相同的错误,但现在可以看到编译器命令行:
D:\arduino-dev\arduino-1.0.3\hardware\tools\avr\bin\avr-g++ -c ...yada yada
more yada... e:\Temp\build3528223623599856131.tmp\template1.cpp ...
template1:14: error: variable or field 'Shift' declared void
template1:14: error: 'TYPE' was not declared in this scope
关键是.CPP文件。这是开发环境从.INO构造的文件,是编译器的实际输入。如果您去抓取该文件,您将看到所有代码,其中包括一些额外的代码行:
#include "Arduino.h"
void Shift(TYPE value);
void setup();
void loop();
为您添加的构建工具,4行:
- Arduino头(因为没有人记得这个)
- 3转发通过解析代码得到的函数声明
您可以通过在ino文件中添加正确的转发声明来消除错误。如果在使用函数之前定义了该函数,则必须执行以下操作:
模板无效移位(类型值);
//您可以在这里使用函数,也可以使用声明
//紧接定义之前
templatevoid Shift(类型值)
{
//您的实现
}
jdr5ca的回答中解释了为什么会出现这种情况。感谢您的澄清,我通过反复试验发现了这一点。编辑问题,将源代码剪辑的文件名包括在内。只需确保模板位于.h文件中,而不是源代码中。
D:\arduino-dev\arduino-1.0.3\hardware\tools\avr\bin\avr-g++ -c ...yada yada
more yada... e:\Temp\build3528223623599856131.tmp\template1.cpp ...
template1:14: error: variable or field 'Shift' declared void
template1:14: error: 'TYPE' was not declared in this scope
#include "Arduino.h"
void Shift(TYPE value);
void setup();
void loop();