Multifile Compilations

I made 4 files but everytime I compile persontest.cpp, I get this:

1
2
3
4
5
/tmp/ccn9tigF.o: In function `main':
persontest.cpp:(.text+0x3e): undefined reference to `Person::Person(std::string, int)'
persontest.cpp:(.text+0x62): undefined reference to `Person::get_name()'
persontest.cpp:(.text+0x6e): undefined reference to `Person::get_age()'
collect2: error: ld returned 1 exit status


I need some help on this. For some reason, the program doesn't listen to the header file or something. I'm pretty sure I set everything right. Any suggestions?

persontest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string>
#include <iostream>
#include "person.h"

using namespace std;

int main()
{

Person rai( "Rai Diaz", 18 );
rai.get_name();
rai.get_age();
return 0;

}


person.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef PERSON_H
#define PERSON_H

#include <string>

using namespace std;


class Person
{
public:
  Person();
  Person(string pname, int page);
  void get_name();
  void get_age();
private:
  int age;
  string name;
 // 0 if unknown
};

#endif 


person.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
#include <iostream>
#include "person.h"

using namespace std;
Person::Person()
{
name = "Initial_Name";
age = 0;
}

Person::Person(string pname, int page)
{
name = pname;
age = page;
}

void Person::get_name() 
{
cout << "Creating personal profile....." << endl;
cout << "Full name: " <<  name  << endl;
}

void Person::get_age()
{
cout << "Age:" << age << endl;
}


Makefile
1
2
3
4
5
6
7
8
9
test: persontest.cpp
	./persontest.out
persontest: persontest.cpp person.o
	g++ -o persontest persontest.cpp person.o
person.o: person.cpp person.h
	g++ -c person.cpp

clean: 
	rm *.o persontest
Use the makefile to build your program.
make persontest works for me.
Last edited on
Thanks!
Topic archived. No new replies allowed.