2014-07-19 40 views
0

我試圖編譯此C使用Arduino的1.0.5-R2 IDE錯誤: 'constexpr' 沒有指定類型間的Arduino IDE

#include <cstdint> 
#include "mcal_reg.h" 

class led 
{ 
public: 
    // Use convenient class-specific typedefs. 
    typedef std::uint8_t port_type; 
    typedef std::uint8_t bval_type; 

    // The led class constructor. 
    led(const port_type p, 
     const bval_type b) : port(p), 
          bval(b) 
    { 
    // Set the port pin to low. 
    *reinterpret_cast<volatile bval_type*>(port) 
     &= static_cast<bval_type>(~bval); 

    // Set the port pin to output. 
    *reinterpret_cast<volatile bval_type*>(port - 1U) 
     |= bval; 
    } 

    void toggle() const 
    { 
    // Toggle the LED via direct memory access. 
    *reinterpret_cast<volatile bval_type*>(port) 
     ^= bval; 
    } 

private: 
    // Private member variables of the class. 
    const port_type port; 
    const bval_type bval; 
}; 

namespace 
{ 
    // Create led_b5 on portb.5. 
    const led led_b5 
    { 
    mcal::reg::portb, 
    mcal::reg::bval5 
    }; 
} 

int main() 
{ 
    // Toggle led_b5 in a loop forever. 
    for(;;) 
    { 
    led_b5.toggle(); 
    } 

++代碼,幷包含文件mcal_reg.h是這樣的:

#ifndef _MCAL_REG_2011_11_04_H_ 
    #define _MCAL_REG_2011_11_04_H_ 

    #include <cstdint> 

    namespace mcal 
    { 
    namespace reg 
    { 
     constexpr std::uint8_t portb = 0x25U; 

     constexpr std::uint8_t bval0 = 0x01U; 
     constexpr std::uint8_t bval1 = 0x01U << 1U; 
     constexpr std::uint8_t bval2 = 0x01U << 2U; 
     constexpr std::uint8_t bval3 = 0x01U << 3U; 
     constexpr std::uint8_t bval4 = 0x01U << 4U; 
     constexpr std::uint8_t bval5 = 0x01U << 5U; 
     constexpr std::uint8_t bval6 = 0x01U << 6U; 
     constexpr std::uint8_t bval7 = 0x01U << 7U; 
    } 
    } 

#endif // _MCAL_REGISTERS_2011_11_04_H_ 

} 

試圖編譯下面的編譯錯誤結果:

mcal_reg.h:17: error: 'constexpr' does not name a type 

參照這一行: constexpr std :: uint8_t portb = 0x25U;

我在包含mcal_reg.h文件的庫文件夾中設置了mcal_reg目錄。這是我第一個Arduino項目,我正在編寫一個程序,以閃存到獨立的AVR芯片。但我無法編譯這個程序。我的系統是Windows 7.並且我只安裝了隨Arduino IDE安裝的軟件。 (沒有單獨的GNU,Visual Studio等等)請幫忙。

+1

?並且請使用IDE正在調用編譯器的命令行更新該問題。 – Praetorian

+0

您可以將IDE更新到測試版http://arduino.cc/en/main/software#toc3?您的版本似乎不支持C++ 11。 – Drek

+0

我不知道我在用什麼編譯器。我是Arduino的新手,正在安裝目錄周圍,但不知道要尋找什麼。是的,我可以嘗試更新。我從Arduino下載了這個IDE,但它與您連接的測試版相比似乎已經過時了。 – KleinBottle

回答

1

要使用像constexpr這樣的C++ 11功能,您需要將IDE更新爲當前的測試版(http://arduino.cc/en/main/software#toc3)。然後通過編譯器標誌-std = C++ 11啓用C++ 11支持。

要添加一個編譯器標誌找到您使用的是什麼編譯platform.txt看到https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5---3rd-party-Hardware-specification和 然後添加正確/改變

## Compiler global definitions 
compiler.path={runtime.ide.path}/tools/avr/bin/ 
compiler.c.cmd=avr-gcc 
compiler.c.flags=-c -g -Os -w -ffunction-sections -fdata-sections -MMD -std=c++11