help with linkage problem

My compiler is g++ and my OS is mac 10.5.8
I don't see an attachment option. I'll just post a summary of my code:
the program is the (beginning of) a game with 2 tanks, one user controlled, on AI, behaviors and sprites controlled by program9handler. p9animationhandler controls the animation and as such holds 2 instances of program9handler. p9animation handler is declared as an instance, or object or whatever in program9rev1.cpp. I did read the long post by disch about headers but its late for me and I don't think I really get it. But this looks right to me. I'm using the external game programming library Allegro, I'm pretty sure that not the problem. The is saying there is a linkage error with p9animationhandler.h
Be easy on me I took one intro class to C++ and the rest I got via diffusion from skimming code online and programming books awhile back. I just need a simple explanation as to what is going on here. Thanks in advance for any help.
terminal output:
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
jdd:allegro-4.2.2 jasondancks$ g++ /users/jasondancks/Desktop/allegrostuff/program/program9rev1.cpp -o /users/jasondancks/Desktop/allegrostuff/program/p9rev `allegro-config --libs`
Undefined symbols:
  "p9animationhandler::setmargin(int)", referenced from:
      _mangled_main()     in ccO9Z6xX.o
  "p9animationhandler::~p9animationhandler()", referenced from:
      ___tcf_0 in ccO9Z6xX.o
  "p9animationhandler::animate()", referenced from:
      draw()    in ccO9Z6xX.o
  "p9animationhandler::go()", referenced from:
      _mangled_main()     in ccO9Z6xX.o
  "p9animationhandler::stop()", referenced from:
      _mangled_main()     in ccO9Z6xX.o
  "p9animationhandler::setbitmaps(BITMAP*)", referenced from:
      _mangled_main()     in ccO9Z6xX.o
  "p9animationhandler::setdelay(int)", referenced from:
      _mangled_main()     in ccO9Z6xX.o
  "p9animationhandler::turnleft()", referenced from:
      _mangled_main()     in ccO9Z6xX.o
  "p9animationhandler::reverse()", referenced from:
      _mangled_main()     in ccO9Z6xX.o
  "p9animationhandler::setuptanks()", referenced from:
      _mangled_main()     in ccO9Z6xX.o
  "p9animationhandler::draw()", referenced from:
      draw()    in ccO9Z6xX.o
  "p9animationhandler::turnright()", referenced from:
      _mangled_main()     in ccO9Z6xX.o
  "p9animationhandler::p9animationhandler()", referenced from:
      __static_initialization_and_destruction_0(int, int)in ccO9Z6xX.o
ld: symbol(s) not found
collect2: ld returned 1 exit status


I'll post a link to the entire code if asked.

program9handler.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef P9_H
#define P9_H
#include <allegro.h>
class program9handler
{
	public:
	program9handler();
	~program9handler();
        ...

	private:
        ...
};
#endif 

program9handler.cpp:
1
2
3
4
5
#include "program9handler.h"
#include <cmath>
#include <stdio.h>
#include <cstdlib>
...

p9animationhandler.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef P9_H
#include "program9handler.h"
#define P9_H
#endif

#ifndef P9A_H
#define P9A_H
class program9handler;
class p9animationhandler
{
	public:
	p9animationhandler();
	~p9animationhandler();
	...

	private:
	...
	program9handler tank1,tank2;
};
#endif 

p9animationhandler.cpp:
1
2
#include "p9animationhandler.h"
...

and the main: program9rev1.cpp
1
2
3
4
5
6
7
8
9
10
#include "p9animationhandler.h"
int height=480;
int width=640;
int margin=40;
int delay=50;
int wallcolor = makecol(50,230,10);
int stop;
BITMAP* scc;
p9animationhandler anim;
...
Did you ever write definitions for setmargin(), animate(), go(), stop(), setbitmaps(), setdelay(), turnleft(), turnright(), reverse(), stuptanks(), draw, p9animationhandler(), and ~p9animationhandler() from the p9animationhandler class?
Yes I did. I wrote it for everything. Let me show you:
p9animationhandler.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
#ifndef P9_H
#include "program9handler.h"
#define P9_H
#endif

#ifndef P9A_H
#define P9A_H
class program9handler;
class p9animationhandler
{
	public:
	p9animationhandler();
	~p9animationhandler();
	
	BITMAP* draw();
	void setbitmaps(BITMAP* b);
	void setmargin(int m);
	void setuptanks();
	void setdelay(int i);
	void turnleft();
	void turnright();
	void reverse();
	void stop();
	void go();
	//void close();
	void animate();

	private:
	int margin;
	BITMAP *sccs[2];
	int ondeck;
	int delay;
	int w,h;
	int color;
	program9handler tank1,tank2;
};
#endif 

p9animationhandler.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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "p9animationhandler.h"

//class program9handler;

p9animationhandler::p9animationhandler()
{
	/*tank1 = new program9handler(m,sc,0);
	tank2 = new program9handler(m,sc,1);
	tank1.setSprite("/users/jasondancks/Desktop/allegrostuff/tank1");
	tank2.setSprite("/users/jasondancks/Desktop/allegrostuff/tank2");
	ondeck = 0;
	tank1.setspeed(10);
	tank2.setspeed(10);*/
	/*sccs[0] = sc;
	int w = sc->w;
	int h = sc->h;
	sccs[1] = create_bitmap(w,h);
	int color = makecol(100,15,250);
	clear_to_color(sccs[0],color);
	clear_to_color(sccs[1],color);*/
}
void p9animationhandler::setbitmaps(BITMAP* b)
{
	sccs[0] = b;
	int w = b->w;
	int h = b->h;
	sccs[1] = create_bitmap(w,h);
	int color = makecol(100,15,250);
	clear_to_color(sccs[0],color);
	clear_to_color(sccs[1],color);

}
void p9animationhandler::setmargin(int m)
{
	margin=m;
	setuptanks();
}
void p9animationhandler::setuptanks()
{
	//tank1 = new program9handler();
	//tank2 = new program9handler();
	tank1.setbitmap(sccs[0]);
	tank2.setbitmap(sccs[0]);
	tank1.setmargin(margin);
	tank2.setmargin(margin);
	tank1.setspeed(10);
	tank2.setspeed(10);
	tank2.setAI();
	tank1.setSprite("/users/jasondancks/Desktop/allegrostuff/tank1");
	tank2.setSprite("/users/jasondancks/Desktop/allegrostuff/tank2");
	ondeck = 0;
	tank1.setspeed(10);
	tank2.setspeed(10);
}
void p9animationhandler::setdelay(int i)
{
	delay = i;
}
void p9animationhandler::animate()
{
	rectfill(sccs[ondeck],0,0,w,h,color);
	tank1.move(tank2);
	tank2.move(tank1);
	draw_sprite(tank1.getbitmap(),sccs[ondeck],tank1.getx(),tank1.gety());
	draw_sprite(tank2.getbitmap(),sccs[ondeck],tank2.getx(),tank2.gety());
	rest(delay);
}
BITMAP* p9animationhandler::draw()
{
	BITMAP* c = sccs[ondeck];
	ondeck = (ondeck+1)%2;
	return c;
}
void p9animationhandler::turnleft()
{
	tank1.changedirection(-1);
}
void p9animationhandler::turnright()
{
	tank1.changedirection(1);
}
void p9animationhandler::reverse()
{
	tank1.goback();
}
void p9animationhandler::stop()
{
	tank1.stoptank();
}
void p9animationhandler::go()
{
	tank1.go();
}
/*void p9animationhandler::close()
{
	tank1.close();
	tank2.close();
	destroy_bitmap(sccs[0]);
	destroy_bitmap(sccs[1]);
}*/
p9animationhandler::~p9animationhandler()
{
	destroy_bitmap(sccs[0]);
	destroy_bitmap(sccs[1]);
}
1
2
g++ -c p9animationhandler.cpp
g++ all-the-other-stuff-you-wrote p9animationhandler.o
Last edited on
Thanks hanst99. I used that, then ld -r to link the object files from the headers into a single .o file, then compiled main with that combined .o and it compiled no errors. Of course I have run-time errors that I'm working through. I posted it all here:

http://www.daniweb.com/software-development/cpp/threads/399040/1711381#post1711381
Topic archived. No new replies allowed.