2013-05-29 68 views
-1

你能幫我用C++寫作嗎? 我有類用戶,誰載: User.h如何用C++編寫構造函數?

class User 
{ 
public: 
    std::string getName(); 
    void changeName(std::string nName); 
    std::string getGroup(); 
    void changeGroup(std::string nGroup); 

    User(std::string nName, std::string nGroup); 
    ~User(void); 
private: 
    std::string name; 
    std::string group; 
}; 

現在我定義類蜜罐:

Honeypot.h:

class Honeypot 
{ 
public: 
    User us; 

我有構造函數:

Honeypot (std::string name, std::string ip, int numberOfInterfaces, std::string os); 

in Honeypot.cpp:

Honeypot::Honeypot(std::string name, std::string ip, int numberOfInterfaces, std::string os):us(nName, nGroup){ 
    this->name = name; 
    this->ip = ip; 
    this-> numberOfInterfaces = numberOfInterfaces; 
    this->os = os; 
} 

但是這種語法不正確。錯誤是:

IntelliSense: expected a ')', 'nGroup' : undeclared identifier and more on line :us(nName, nGroup){... 

謝謝你的幫忙。

+0

您沒有將'nGroup'或'nName'定義爲參數。 –

回答

1

nName和nGroup需要是Honeypot構造函數的參數;正如編譯器指出的那樣,它們是未聲明的。

Honeypot::Honeypot(std::string name, std::string ip, 
        int numberOfInterfaces, std::string os, 
        std::string userName, std::string userGroup) 
    : us(userName, userGroup) 
{ 
    this->name = name; 
    this->ip = ip; 
    this->numberOfInterfaces = numberOfInterfaces; 
    this->os = os; 
} 
+0

謝謝,我爲我的愚蠢問題感到抱歉。我可能厭倦了...... – Mato