尝试使用模板,而不是在一个Arduino的重载函数:在这个范围类型没有声明 [英] Attempting to use a Template instead of an Overloaded Function in Arduino: TYPE not declared in this Scope

查看:346
本文介绍了尝试使用模板,而不是在一个Arduino的重载函数:在这个范围类型没有声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写能够数据移出到可以移出8,16和32位值74HC595移位寄存器的功能。

I'm trying to write a function that can Shift out data to 74HC595 shift registers which can shift out 8, 16, and 32 bit values.

使用重载函数,我有这样的:

Using an overloaded function, I have this:

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

和我想用这个模板来替换这些功能:

And I want to use this template to replace these functions:

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

当我编译,我得到以下错误:

When I compile, I get the following errors:

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

我是什么做错了吗?

What am I doing wrong?

推荐答案

好吧,这属于在提防开发工具轴承礼物的类别。 Arduino的草图工具是你的问题。如果你打开​​preferences菜单上详细的编译器输出,你会得到一些洞察到发生了什么。有了您的code,我可以复制你的错误。当编译叫template1的一个测试项目,同样的错误报道,但现在可以看到编译器命令行:

Okay, this falls in the category of "beware of dev tools bearing gifts". The Arduino sketch tool is your problem. If you turn on verbose compiler output on the preferences menu, you will get some insight into what happens. With your code, I can duplicate your error. When compiling a test project called template1, the same error is reported, but now can see the compiler command line:

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构造并是什么是实际输入到编译器的一个文件。如果你去抓住这个文件,你会看到所有的code一些奖励范围包括:

The key point is that .CPP file. That is a file that the dev environment constructs from your .INO and is what is the actual input to the compiler. If you go grab that file, you will see all your code with some bonus lines included:

#include "Arduino.h"
void Shift(TYPE value);
void setup();
void loop();

构建工具为你添加,4行:

The build tool added for you, 4 lines:


  • Arduino的头(因为没人记得这一点)

  • 3前锋的声明,它通过解析code
  • 想通了功能

从函数模板产生一个向前声明的尝试是不正确,并产生code,它导致编译错误。

The attempt at producing a forward declaration from the function template is incorrect, and produces the code that results in the compiler error.

的解决方案是从.INO文件出移动模板。

The solution is to move the template out from the .INO file.


  1. 创建一个库文件夹,说T1。

  2. 创建与模板code该文件夹中.h文件,说tspi.h.

  3. 导入库到您的项目。

  4. 确保#include行为code在你的.INO(更古怪的第一行之后 - 该工具所有的意见之后,但在$ C的第一行之前将插入一个的#includeArduino.h $ c。如果你离开你包括在.INO文件的顶部,将Arduino的头前处理)

这篇关于尝试使用模板,而不是在一个Arduino的重载函数:在这个范围类型没有声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆