如何用C ++编写构造函数?

时间:2013-05-29 22:28:28

标签: c++ visual-studio-2010 visual-c++

你可以帮我解决C ++中的编写问题吗? 我有班级用户,其中包含: User.h

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类中定义:

Honeypot.h:

class Honeypot
{
public:
    User us;

我有构造函数:

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

在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){...

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

name和Group需要是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;
}