Sean M
Sean M

Reputation: 31

How to construct an object with arguments in c++

I keep getting this error: no matching function for call to 'Person::Person(const char [10]) The class is in a separate cpp file. I can easily create an object when I have the constructor in the same cpp file. This is my code:

main.cpp file

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

using namespace std;

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

Person.h file

#ifndef PERSON_H
#define PERSON_H


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

#endif // PERSON_H

Person.cpp file

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

using namespace std;

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

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

Upvotes: 0

Views: 107

Answers (1)

RiaD
RiaD

Reputation: 47619

you have to add declaration of second constructor in your .h file

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

Upvotes: 6

Related Questions