Is this name collision?

The error was "no instance of overloaded function "people::people" matches the specified type" on the first line of code of my C++ file. People::people... I included my cpp file first, then header file. Please let me know if you need further information.

the code is trying to pass string n to name, and bo from birthday class to dateOfBirth. Please ONLY look at code people::people(string n, string bo) and :name(n), dateOfBirth(bo). That's the only error I have. Other codes are reference.

//C++ file below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "people.h"
#include "birthday.h"
#include <iostream>

using namespace std;

people::people(string n, string bo)
:name(n), dateOfBirth(bo)
{
}

void people::printInfo(){
    cout<<name<<" was born on";
    dateOfBirth.printDate();
}

int main()
{
    birthday birthdayObj(1,11,1994);
    people JunweiC("junwei the king", birthdayObj);
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

//Header file below
#ifndef PEOPLE_H // if not to find Sally header file
#define PEOPLE_H


#include <string>
#include "birthday.h"
using namespace std;

class people
{
    public:
        people(string n, birthday bo);
        void printInfo();
    private:
        string name;
        birthday dateOfBirth;
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include "birthday.h"

using namespace std;
    


birthday::birthday(int m, int d, int y)
{
    month = m;
    day = d;
    year=y;
}

void birthday::printDate(){
    cout << month << "/" << day <<  "/" << year << endl;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef BIRTHDAY_H // if not to find Sally header file
#define BIRTHDAY_H //Then define it

class birthday
{

    public:
        birthday(int m, int d, int y);
        void printDate();
    private:
        int month;
        int day;
        int year;
};

#endif 



Last edited on
people JunweiC("junwei the king", birthdayObj)

You're trying to create a people object, using a constructor that accepts a string (actually const char* , but the conversion gets done for you) and a birthday object.

Let's see the constructor code you actually wrote:

1
2
3
4
people::people(string n, string bo)
:name(n), dateOfBirth(bo)
{
}


Well this appears to be a constructor that accepts a string and another string. Where is the code implementing the constructor that accepts a string and a birthday?


My goal is letting "bo" takes birthday like this : birthday birthdayObj(1,11,1994) to birthday dateOfBirth. See code below.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef PEOPLE_H 
#define PEOPLE_H
#include <string>
#include "birthday.h"
using namespace std;

class people
{
    public:
        people(string n, birthday bo);
        void printInfo();
    private:
        string name;
        birthday dateOfBirth;
};

#endif  
Last edited on
Update

i changed people::people(string n, string bo) (7th line of first chunk of code) to people::people(string n, birthday bo). The error was gone but the code doesn't run. The error message is
undefined reference to `birthday::printDate()
&
undefined reference to `birthday::birthday(int,
int, int)




Updated code below (first chunk I posted)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "people.h"
#include "birthday.h"
#include <iostream>

using namespace std;

people::people(string n, birthday bo)
:name(n), dateOfBirth(bo)
{
}

void people::printInfo(){
    cout<<name<<" was born on";
    dateOfBirth.printDate();
}

int main()
{
    birthday birthdayObj(1,11,1994);
    people JunweiC("junwei the king", birthdayObj);
    JunweiC.printInfo();
}
Looks like you did not compile and link the file that contains this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include "birthday.h"

using namespace std;
    


birthday::birthday(int m, int d, int y)
{
    month = m;
    day = d;
    year=y;
}

void birthday::printDate(){
    cout << month << "/" << day <<  "/" << year << endl;
}
This compiles and runs OK as 1 file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <string>
#include <iostream>

class birthday
{
public:
	birthday(int m, int d, int y);
	void printDate();
private:
	int month {};
	int day {};
	int year {};
};

class people
{
public:
	people(const std::string& n, const birthday& bo);
	void printInfo();
private:
	std::string name;
	birthday dateOfBirth;
};

birthday::birthday(int m, int d, int y) : month(m), day(d), year(y) {}

void birthday::printDate() {
	std::cout << month << "/" << day << "/" << year << '\n';
}

people::people(const std::string& n, const birthday& bo) : name(n), dateOfBirth(bo) {}

void people::printInfo() {
	std::cout << name << " was born on ";
	dateOfBirth.printDate();
}

int main()
{
	birthday birthdayObj(1, 11, 1994);
	people JunweiC("junwei the king", birthdayObj);

	JunweiC.printInfo();
}

Last edited on
Topic archived. No new replies allowed.