2016-03-28 36 views
0

我對此很新,所以如果我打電話給某個錯誤的名稱,請原諒我。我想要做的是將一個類的實例傳遞給另一個類的構造函數。我知道這些通常是在.h和.cpp文件中完成的,但是對於我運行的代碼,它似乎並不在意,但我可能會錯誤。除了類defs和構造函數外,我已經取出了大部分代碼。Arduino傳入一個對象作爲另一個類的構造函數中的參數

我想在我的代碼中有一些像thermtherm這樣的熱敏電阻的實例,並傳遞給Tempcontroller的構造函數,所以我可以調用coldtherm,就像我在printfromthermistor函數中顯示的一樣。

//Thermistor Class 
    class Thermistor 
{ 

    int Thermpin; 

public: 
    Thermistor(int pin) 
    { 
    Thermpin = pin; 
    } 


double TEMPOutput() 
    { 
    return Thermpin; 
    } 
void Update() 
    { 

    } 
}; 

Thermistor coldtherm(1); 

//Tempcontrol Class 
class TempController 
{ 

public: 

TempController(Thermistor&) //Right here I want to pass in coldtherm to the Tempcontroller and be able to call functions from that class. 


void printfromthermistor() 
{ 

    Thermistor.TEMPOutput(); 
} 


}; 

回答

0

重複的this

引用只能初始化,不能更改。要在構造函數中使用它,就像您顯示的那樣,意味着參考成員必須在構造函數中初始化爲:

class TempController 
{ 
    Thermistor & member; 
public: 
    TempController(Thermistor & t) { member = t; }; // assignment not allowed 
    TempController(Thermistor & t) : member(t) { }; // initialization allowed 
} 
相關問題