C++ classes

hi
I am trying on a simple c++ program. I am trying to make flights related program. It has four files main.cpp, flight.cpp, flight.h, clock.h.

When i run this program it displays this error
1
2
3
1>flight.obj : error LNK2019: unresolved external symbol "public: void __thiscall Clock::write(bool)" (?write@Clock@@QAEX_N@Z) referenced in function "public: void __thiscall Flight::info(void)" (?info@Flight@@QAEXXZ)
1>flight.obj : error LNK2019: unresolved external symbol "public: void __thiscall Clock::tick(void)" (?tick@Clock@@QAEXXZ) referenced in function "public: void __thiscall Flight::delay(int)" (?delay@Flight@@QAEXH@Z)
1>D:\C++ Projects\flights\Debug\flights.exe : fatal error LNK1120: 2 unresolved externals


here is my code
main.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "flight.h"

int main() 
{
	Flight f;
	f.init("SK1853", 8, 10, 10, 55);
	f.delay(15);
	f.info();
	system("PAUSE");
	return 0;
}


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

class Flight {
public:
	void init(string flight_no, int dep_h, int dep_m, int arr_h, int arr_m);
	void info();
	void delay(int min);

private:
	string no;
	Clock dep, arr;
};

#endif 


flight.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
#include "flight.h"
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void Flight::init(string flight_no, int dep_h, int dep_m, int arr_h, int arr_m)
{
	no = flight_no;
	dep.set(dep_h, dep_m, 0);
	arr.set(arr_h, arr_m, 0);
}

void Flight::info()
{
	cout << "Flight no " << no;
	cout << ", Dep "; dep.write(false);
	cout << ", Arr "; arr.write(false);
	cout << endl;
}

void Flight::delay(int min)
{
	for(int i = 0; i <= min*60; i++)
		dep.tick();
	for(int j = 0; j <= min*60; j++)
		arr.tick();
}


clock.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
class Clock
{
public:
	void set(int hour, int min, int sec);
	int read_hour() {
		return h;
	}
	int read_min() {
		return m;
	}
	int read_sec() {
		return s;
	}
	void write(bool write_sec = true);
	void tick();

private:
	int h,m,s;
};

inline void Clock::set(int hour, int min, int sec)
{
	h = hour;
	m = min;
	s = sec;
}


I am using Microsoft Visual C++ 2010 Express

Please help
Unresolved external means that you've declared a function but not given it a body. Give Clock::tick() and Clock::write() a body just like you gave Clock::set a body here

1
2
3
4
5
6
inline void Clock::set(int hour, int min, int sec)
{
	h = hour;
	m = min;
	s = sec;
}


even if you aren't going to use those functions yet they need bodies.
Last edited on
I changed clock.h to this
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
#include <iostream>
#include <iomanip>
using namespace std;

class Clock
{
public:
	void set(int hour, int min, int sec);
	int read_hour() {
		return h;
	}
	int read_min() {
		return m;
	}
	int read_sec() {
		return s;
	}
	void write(bool write_sec = true);
	void tick();

private:
	int h,m,s;
};

inline void Clock::set(int hour, int min, int sec)
{
	h = hour;
	m = min;
	s = sec;
}

void Clock::tick()
{
	s = (s+1) % 60;
	if(s == 0) {
		m = (m+1) % 60;
		if(m == 0) {
			h = (h+1) % 60;
		}
	}
}

void Clock::write(bool write_sec)
{
	cout << setw(2) << setfill('0') << h
		<< ':' << setw(2) << setfill('0') << m;
	if(write_sec)
		cout << ':' << setw(2) << setfill('0') << s;
}


now the error is

1
2
3
1>main.obj : error LNK2005: "public: void __thiscall Clock::tick(void)" (?tick@Clock@@QAEXXZ) already defined in flight.obj
1>main.obj : error LNK2005: "public: void __thiscall Clock::write(bool)" (?write@Clock@@QAEX_N@Z) already defined in flight.obj
1>D:\C++ Projects\flights\Debug\flights.exe : fatal error LNK1169: one or more multiply defined symbols found
function bodies either need to be inline, or they need to exist in a cpp file (not in a header).

Either make Clock::tick and Clock::write inline (like Clock::set) or put them in a separate .cpp file.
Topic archived. No new replies allowed.