Trouble seperating file into multiple classes

I'm trying to separate this program into different files: bike.cpp, bike.h, wheel.cpp, wheel.h. If I leave everything in one file it compiles and runs fine, but when I separate it I get the following error.

/tmp/cc8SG45c.o: In function `bike::bike(unsigned int, unsigned int)':
bike.cpp:(.text+0x1c): undefined reference to `wheel::wheel(unsigned int)'
bike.cpp:(.text+0x34): undefined reference to `wheel::wheel(unsigned int)'
/tmp/cc8SG45c.o: In function `bike::getTirePressure(unsigned char)':
bike.cpp:(.text+0x7a): undefined reference to `wheel::get_Pressure()'
bike.cpp:(.text+0x88): undefined reference to `wheel::get_Pressure()'
collect2: ld returned 1 exit status

This is the code together
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  #include <stdio.h>


class wheel
{
	
	public:
		wheel(unsigned int p);
		unsigned int get_Pressure( void );
	private:
		unsigned int pressure;
};


wheel::wheel(unsigned int p)
{
	printf("Wheel creation.\n\r");
	pressure = p;
}

unsigned int wheel::get_Pressure( void )
{
	return pressure;
}



#define FRONT_TIRE 1
#define BACK_TIRE  2

class bike
{
	
	public:
		bike(unsigned int fp, unsigned int bp);
		unsigned int getTirePressure(unsigned char t);
		wheel* front;
		wheel* back;	

	private:
};


bike::bike(unsigned int fp, unsigned int bp)
{
	this->front = new wheel(fp);
	this->back = new wheel(bp);
}

unsigned int bike::getTirePressure(unsigned char t)
{
	switch(t)
	{
		case FRONT_TIRE:
			return front->get_Pressure();
			break;
		case BACK_TIRE:
			return back->get_Pressure();
			break;
		default:
			return -1;
			break;
	}
}

int main()
{
	unsigned int pf; //Pressure Front Tire
	unsigned int pb; //Pressure Back Tire

	pf = 25;
	pb = 26;

	bike b(pf,pb);
	pf = b.getTirePressure(1);
	pb = b.getTirePressure(2);

	printf("Front Tire Pressure %d\n\r",pf);
	printf("Back Tire Pressure %d\n\r", pb);
}



This is the code seperate
bike.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
38
39
40
41
42
43
  #include <stdio.h>
#include "wheel.h"
#include "bike.h"


bike::bike(unsigned int fp, unsigned int bp)
{
	this->front = new wheel(fp);
	this->back = new wheel(bp);
}

unsigned int bike::getTirePressure(unsigned char t)
{
	switch(t)
	{
		case FRONT_TIRE:
			return front->get_Pressure();
			break;
		case BACK_TIRE:
			return back->get_Pressure();
			break;
		default:
			return -1;
			break;
	}
}

int main()
{
	unsigned int pf; //Pressure Front Tire
	unsigned int pb; //Pressure Back Tire

	pf = 25;
	pb = 26;

	bike b(pf,pb);
	pf = b.getTirePressure(1);
	pb = b.getTirePressure(2);

	printf("Front Tire Pressure %d",pf);
	printf("Back Tire Pressure %d", pb);
}



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

#include "wheel.h"

#define FRONT_TIRE 1
#define BACK_TIRE  2

class bike
{
	
	public:
		bike(unsigned int fp, unsigned int bp);
		unsigned int getTirePressure(unsigned char t);
		wheel* front;
		wheel* back;	

	private:
};
	
#endif 


wheel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include "wheel.h"


wheel::wheel(unsigned int p)
{
	printf("Wheel creation.");
	pressure = p;
}

unsigned int wheel::get_Pressure( void )
{
	return pressure;
}


wheel.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef WHEEL_H
#define WHEEL_H

#include <stdio.h>

class wheel
{
	
	public:
		wheel(unsigned int p);
		unsigned int get_Pressure( void );
	private:
		unsigned int pressure;
};

#endif 
Last edited on
How do you compile the separate version?
sdrollinger,

In addition to keskiverto's question I would add: in what order are you compiling the files, and what compiler are you using?

In the file that is one file wheel is defined before bike, but in separate files I am thinking that if you compile bike before wheel you will have the problem bike will know nothing about wheel.

Knowing which compiler or IDE you are using will help.

Andy
I am thinking that if you compile bike before wheel you will have the problem bike will know nothing about wheel.

No. The bike (cpp) knows everything that it needs to know about wheel, because it sees the definition of wheel (i.e. includes wheel.h).
sdrollinger,

I just finished putting your code into Visual Studio 2015. While I was copying and putting everything into separate files I noticed that the section for bike.cpp contained the main function. Either you have put main in the wrong file (it should be in what is considered the main file for the project) or you have have duplicated the code for main which would be a compile error being that you are trying to redefine main.

Once I moved the main function from bike.cpp to where it should be the program ran fine.

Hope that helps,

Andy
Either you have put main in the wrong file (it should be in what is considered the main file for the project) or you have have duplicated the code for main which would be a compile error being that you are trying to redefine main.

Again, the main() can be in any compilation unit, although it is more intuitive if it is not in what (by name) looks "just a class implementation".

If same code (implementation) is in more than one unit, then the linker will give a "multiple definitions" error.

The OP, however, had "missing definitions" errors, which also come from the linking process (rather than actual compiler). The compiler has to be told to produce an object file from each compilation unit (.cpp file) and then the linker has to be given all those object files (and used libraries, which are precompiled collections of object code). The linker reports "undefined reference" if it is not given all the relevant object code.

Either no object file was compiled from the wheel.cpp or that object file was not a parameter in the linker call.


One can run all the necessary compilation command manually from command line. That gets tedious.

There is a family of "make" commands that read and execute compilation commands from "makefile". The makefile is a recipe to compile and make runs only the necessary subcommands. For example, changing wheel.cpp and calling make will recompile only the wheel and link, if bike.cpp was not changed and its "up to date" object file still exists.

The makefile is specific to a platform and semitedious to write manually. There are multiple tools (e.g. GNU build system, cmake, qmake) that generate a makefile suitable for the platform from their own recipes. An IDE and its "project" is such a tool.
Topic archived. No new replies allowed.