简体   繁体   English

C ++需要使用自定义构造函数的帮助

[英]C++ Need help using custom constructor

new to c++ and cant figure out why visual studio doesn't like my "HealthProfile person.setFirstName(first)" line of code. C ++的新增功能,无法弄清Visual Studio为什么不喜欢我的“ HealthProfile person.setFirstName(first)”代码行。 The error is with "person" and the error is no default constructor. 错误与“人”有关,并且该错误不是默认构造函数。 It's probably something painfully obvious, but my code is almost identical from the code in my book. 这可能很痛苦,但我的代码与书中的代码几乎相同。 Thanks in advance! 提前致谢!

main: 主要:

#include <iostream>
#include "HealthProfile.h"
using namespace std;

int main()
{
    string first;
    HealthProfile person;

    cout << "Enter first name" << endl;
    getline(cin,first);
    person.setFirstName(first);

}

header: 标头:

#include <string>
using namespace std;

class HealthProfile
{
public:
    HealthProfile(string, string, string, int, int, int, int, int);
    void setFirstName(string);
    string getFirstName();
    void setLastName(string);
    string getLastName();
};

function: 功能:

#include <iostream>
#include "HealthProfile.h"

using namespace std;

HealthProfile::HealthProfile(string first, string last, string g,
    int m, int d, int y, int h, int w)
{
    setFirstName(first);
    setLastName(last);
    setGender(g);
    setMonth(m);
    setDay(d);
    setYear(y);
    setHeight(h);
    setWeight(w);
}

void HealthProfile::setFirstName(string first)
{
    firstName = first;
}
string HealthProfile::getFirstName()
{
    return firstName;
}
void HealthProfile::setLastName(string last)
{
    lastName = last;
}
string HealthProfile::getLastName()
{
    return lastName;
}

There is nothing wrong with setFirstName() . setFirstName()没有任何问题。 The issue is that you've declared a constructor that takes three strings and five ints , this removes the default constructor which you are using when you call HealthProfile person 问题是您已声明一个构造函数,该构造函数采用三个strings和五个ints ,这会删除您在调用HealthProfile person时使用的默认构造函数

The solution is to either use the HealthProfile cosntructor and pass it three strings and five ints , or to declare and define a constructor that takes no parameters by adding HealthProfile(){} to the header. 解决方案是使用HealthProfile cosntructor并将其传递三个strings和五个ints ,或者通过将HealthProfile(){}添加到标头来声明并定义一个不带参数的构造函数。

This line in main: 此行主要:

HealthProfile person;

Declares an instance of the HealthProfile class using a default constructor. 使用默认构造函数声明HealthProfile类的实例。 You've not declared a default constructor. 您尚未声明默认构造函数。 Creating your own custom constructor prevents a default constructor from being implicitly created for you. 创建自己的自定义构造函数可防止为您隐式创建默认构造函数。 If you want to use a default constructor as well as a custom one, you need to explicitly declare and define it. 如果要使用默认构造函数和自定义构造函数,则需要显式声明和定义它。 If you don't want to use a default constructor, then pass in arguments to use your custom one. 如果您不想使用默认的构造函数,则传入参数以使用自定义的构造函数。

To declare a default constructor in your .h: 要在您的.h中声明默认构造函数,请执行以下操作:

HealthProfile();

And to define it in your .cpp: 并在您的.cpp文件中定义它:

HealthProfile::HealthProfile() { }

Or to simply call your existing custom constructor in main: 或者简单地在main中调用您现有的自定义构造函数:

HealthProfile person(first,last,g,m,d,y,h,w);  // AFTER collecting values for these arguments

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM