小编Sea*_*n M的帖子

如何用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;
}
Run Code Online (Sandbox Code Playgroud)

Person.h文件

#ifndef PERSON_H
#define PERSON_H


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

#endif // PERSON_H
Run Code Online (Sandbox Code Playgroud)

Person.cpp文件

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

using namespace std;

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

Person::Person(string n)
{
   cout << n;
}
Run Code Online (Sandbox Code Playgroud)

c++ constructor

0
推荐指数
1
解决办法
91
查看次数

标签 统计

c++ ×1

constructor ×1