2013-05-29 65 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