struct delcaration调用错误的构造函数

时间:2017-09-05 19:37:07

标签: c++

这是我第一次使用struct。我需要在main中初始化secure :: secure(),但不知道我应该放在那里。它需要一个默认的构造函数,它会跳过我所做的所有声明。

我该怎么办?

此外,我的代码可能还有其他问题,我不会感到惊讶。现在,请解释我如何引用特定声明而不是默认声明。

@ECHO OFF
SETLOCAL
SET HOME=%~dp0
"%~dp0\influx.exe" %*
ENDLOCAL

1 个答案:

答案 0 :(得分:0)

您创建了一个构造函数,该构造函数需要5个输入参数(并且您不会在代码中的任何位置使用它)。当您在main()中创建结构的实例时,它会尝试调用没有参数的构造函数,并且会失败,因为如果您没有默认构造函数,那么您必须按照自己的方式创建它。只需创建一个没有参数的构造函数,它应该可行。

struct secure
{
    string surname;
    string name;
    string age;
    string placeofbirth;
    string tel;
    secure(const string& s, const string& n, string a, const string& p, const string& t);
    secure(); //Defaul constr with no args
};

secure::secure()
{ 
    //you can do something here if you want
}

如果你想使用你的5 arg。构造函数,你必须这样声明:

secure exotic(arg1, arg2, arg3, arg4, arg5);