繁体   English   中英

如何在C ++中构造带有参数的对象

[英]How to construct an object with arguments in c++

我不断收到此错误: no matching function for call to 'Person::Person(const char [10])该类位于单独的cpp文件中。 当我在同一cpp文件中包含构造函数时,可以轻松创建对象。 这是我的代码:

main.cpp文件

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

using namespace std;

int main()
{
    Person p("hellooooo");
    return 0;
}

Person.h文件

#ifndef PERSON_H
#define PERSON_H


class Person
{
    public:
        Person();
    protected:
    private:
};

#endif // PERSON_H

Person.cpp文件

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

using namespace std;

Person::Person()
{
   cout << "this is the default constructor??";
}

Person::Person(string n)
{
   cout << n;
}

您必须在.h文件中添加第二个构造函数的声明

#include <string>
class Person
{
    public:
        Person();
        Person(std::string);
};

暂无
暂无

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

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