Undefined reference to operator >>

Hi everyone,
Im writing 2 classes. I have tested class Person. It is fine.
Then I write class Company which use Person as part of it. But Im encountering on problem:
Company_test.cpp:(.text+0xdf): undefined reference to `operator<<(std::ostream&, Person const&)'

C:\DevC++\collect2.exe [Error] ld returned 1 exit status

what is wrong? Is it because of compiler? I am using dev c++
Thank you.

Person.h
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#ifndef PERSON_H_
#define PERSON_H_
#include <string>
#include <iostream>

class Person
{
	public:
	//constructor
	Person(std::string first, std::string family, std::string ID, int birth): given_name(first), family_name(family), ID_number(ID), birth_year(birth){}
	
	//default
	Person():given_name(""),family_name(""),ID_number(""),birth_year(1900){}
	
	//modifier functions
	void set_given_name(std::string given)
	{
		given_name=given;
	}
	
	void set_family_name(std::string family)
	{
		family_name=family;
	}
	
	void set_birth_year(int birth)
	{
		birth_year=birth;
	}
	
	//access function
	std::string get_given_name() const {return given_name;}
	
	std::string get_family_name() const {return family_name;}
	
	std::string get_ID_number() const {return ID_number;}
	
	int get_birth_year() const {return birth_year;}
	
	int age(int year) const;
	
	bool can_vote(int year) const;
	
	bool is_senior(int year) const;
	
	bool operator==(const Person& per) const;
	
	bool operator!=(const Person& per) const;
	
	friend std::ostream& operator<<(std::ostream& os, const Person& per);
	
	private:
	
	std::string given_name;
	std::string family_name;
	std::string ID_number;
	int birth_year;
	
	//constants
	static const int VOTE_AGE=18;
	static const int SENIOR_AGE=65;
};

#endif 


Company.h
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
#ifndef COMPANY_H_
#define COMPANY_H_

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

class Company
{
	private:
		int num_employees;
		Person* employees;
		static const int DEFAULT_SIZE=100;
		
	public:
	//constructor
	Company(int size):num_employees(size),employees(new Person[size]){}
	//default constructor
	Company():num_employees(DEFAULT_SIZE),employees(new Person[DEFAULT_SIZE]){}
	
	void set_employee(int index, Person emp);
	
	Person get_employee(int index);
	
	friend std::ostream& operator<<(std::ostream& os, const Company& company);	
};

#endif 


Company_test.cpp
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
#include "Company.h"
#include <iostream>
#include <ostream>
//using namespace std; 
using std::ostream;
using std::endl;
using std::cout;

void Company::set_employee(int index, Person emp)
{
	if(index>=0&&index<num_employees)
		employees[index]=emp;
}

Person Company::get_employee(int index)
{
	if(index>=0&&index<num_employees)
		return employees[index];
	else
		return Person();
}

ostream& operator<<(ostream& os, const Company& company)
{
	for(int i=0;i<company.num_employees;i++)
		os<<company.employees[i]<<endl;
	return os;
}

int main()
{
	Company comp(2);
	comp.set_employee(0,Person("Elly","K","123",1942));
	comp.set_employee(1,Person("Davy","K","124",1943));
	cout<<comp;
	return 0;
}

Last edited on
Oh silly me!
I define operator<<(std::ostream&, Person const&) in my previous testing file. I forgot add this line in company_test.cpp

add below in company_test
ostream& operator<<(std::ostream& os, const Person& per)
{
os<<"Given name: "<<per.given_name<<'\n'
<<"Family name: "<<per.get_family_name()<<'\n'
<<"ID number: "<<per.ID_number<<'\n'
<<"Year of birth: "<<per.get_birth_year()<<'\n';
return os;
}
Topic archived. No new replies allowed.