0

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;
}

1 Answer 1

6

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

#include <string>
class Person
{
    public:
        Person();
        Person(std::string);
};
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, and +1. (Also, I edited the question... Terminology, terminology everywhere... :P)
+1, or change the constructor to Person(const std::string& n = "This is the constructor") and just cout << n;` in the body. Either way works.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.