gpt4 book ai didi

templates - 尝试在 Arduino : TYPE not declared in this Scope 中使用模板而不是重载函数

转载 作者:行者123 更新时间:2023-12-01 09:59:43 27 4
gpt4 key购买 nike

我正在尝试编写一个可以将数据移出到 74HC595 移位寄存器的函数,该寄存器可以移出 8、16 和 32 位值。

使用重载函数,我有这个:

/**********************************************************************************
* 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;
}

我想用这个模板来替换这些函数:

template<typename TYPE>void Shift(TYPE value)
{
uint8_t loops = (( 8 * sizeof(value) ) - 1 );

LATCH;
for (uint8_t i = 0; i <= loops; 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;
}

编译时出现如下错误:

 Compiling 'zLEDArray' for 'Arduino Uno' 
zLEDArray.ino : variable or field 'Shift' declared void
zLEDArray.ino : 'TYPE' was not declared in this scope
Error compiling

我做错了什么?

最佳答案

好吧,这属于“提防带有礼物的开发工具”的范畴。 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 header (因为没有人记得这个)
  • 3 个通过解析代码找出的函数的前向声明

从函数模板生成前向声明的尝试不正确,并生成导致编译器错误的代码。

解决方案是将模板从 .INO 文件中移出。

  1. 创建一个库文件夹,比如 T1。
  2. 使用模板代码在该文件夹中创建一个 .H 文件,例如 tspi.h。
  3. 将库导入您的项目。
  4. 确保#include 行在您的 .INO 中的第一行代码之后(更奇怪 - 该工具将在所有注释之后但在第一行代码之前插入#include“Arduino.h”。如果你离开您包含在 .INO 文件的顶部,它将在 Arduino header 之前处理)

关于templates - 尝试在 Arduino : TYPE not declared in this Scope 中使用模板而不是重载函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18052310/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com