2014-11-21 55 views
0

我基本上试图初始化一个外部类,以便在我的所有方法中使用它作为成员。从外部类创建成员

我的尝试:

  • 初始化在头文件中的成员(错误:error: 'RECV_PIN_1' is not a type
  • 在构造函数初始化它(现在是不是可以在我的方法)

这里的我缩短的代码:

// Receiver.h 
#include "Arduino.h" 
#include "IRremote.h" 

class Receiver { 
public: 
    Receiver(); 
    void tick(); 
private: 
    static const int LED_PIN = 13; 
    static const int RECV_PIN_1 = 11; 
    static const int MAX_HEALTH = 1000; 

    // [..] 

    IRrecv irrecv(RECV_PIN_1); // this does not work 

    // [..] 
}; 


// Receiver.cpp 
#include "Arduino.h" 
#include "IRremote.h" 
#include "Receiver.h" 

Receiver::Receiver() { 
    // [..] 
} 

void Receiver::tick() { 
    checkHitIndicator(); 
    // if there is a result 
    if (irrecv.decode(&results)) { 
     playerHitDetected(10); 
     // receive the next value 
     irrecv.resume(); 
    } 
} 

这会很好,如果somebod你可以解释我是如何以及为什么会达到这个目标的。

回答

0

第一种方法只有在您的编译器支持C++ 11类内初始化时才有效;你需要={}初始化它,因为初始化与()看起来太像一个函数声明:

IRrecv irrecv{RECV_PIN_1}; // or 
IRrecv irrecv = IRecv(RECV_PIN_1); 

第二种方法应该罚款;我在类定义

IRecv irrecv; 

已经不知道为什么它可能不是在你的可用方法,只要你已经声明它(不初始化),并在构造函数初始化它

Receiver::Receiver() : irrecv(RECV_PIN_1) { 
    // [..] 
} 
+0

嗨,我的编译器不支持第一种方法。 第二个抛出一个神秘的错误'未定义的引用'IRrecv :: IRrecv(int)'' – 2014-11-21 10:18:21

+0

@JulianHollmann:第二个应该是,这是从时间的黎明以来的标准。 – 2014-11-21 10:18:56

+0

我更新了我的评论,第二种方法也不适用于我 – 2014-11-21 10:20:44