My pointer argument won't accept a byref to a pointer

I can't compile my code.

Hey guys, I'm a little overwhelmed so I will try and break it down.

See, I have 3 classes that have a hierchy like this.
_________________________________________
| |
| is-a is-a |
| Pirate --> Sailor --> GameObject. |
|________________________________________|


Where the rightmost class is also the most Base.


So, I need to do a lot of generic programming. In this assignment. Well, I also need to store everything in linked lists. This is where it starts to get messy.

So I'm going to compare two pieces of code, the top one works but the bottom one will not allow me to compile.

Also note that Dock is just one generation from GameObject.
1
2
3
4
5
6
7
8
9
10
11
12
13
dock_ptrs.push_back(new Dock(1, CartPoint(5, 1)));
        
        // Object_ptrs and Active_ptrs are both List<GameObject*>
        // Dock_ptrs is List<Dock*>

	object_ptrs.push_back(dock_ptrs.back()); // back() Retrieves backItem without iterator
	active_ptrs.push_back(dock_ptrs.back()); 
	dock_ptrs.push_back(new Dock(2, CartPoint(6, 2)));
	object_ptrs.push_back(dock_ptrs.back());
	active_ptrs.push_back(dock_ptrs.back());
	dock_ptrs.push_back(new Dock(3, CartPoint(1, 8)));
	object_ptrs.push_back(dock_ptrs.back());
	active_ptrs.push_back(dock_ptrs.back());



So that one works, but this bottom one on the other hand doesn't.

1
2
3
4
5
6
7
8
9
10
11
12

        // pirate_ptrs is List<Pirate*>
        // sailor_ptrs is List<Pirate*>        

        pirate_ptrs.push_back(new Pirate(1, this));
	sailor_ptrs.push_back(pirate_ptrs.back());
	object_ptrs.push_back(pirate_ptrs.back());
	active_ptrs.push_back(pirate_ptrs.back());
	pirate_ptrs.push_back(new Pirate(1, this));
	sailor_ptrs.push_back(pirate_ptrs.back());
	object_ptrs.push_back(pirate_ptrs.back());
	active_ptrs.push_back(pirate_ptrs.back());


My output summarizes into two things.

Error	19	error C2504: 'Sailor' : base class undefined



Error	35	error C2664: 'void std::list<GameObject *,std::allocator<_Ty>>::push_back(const _Ty &)' : cannot convert argument 1 from 'Pirate *' to 'GameObject *&&


I'm not sure what's going on! It seems like these might even be two separate errors! But I just can't say because nothing works right now.

Here is my code. Because this is from my university, I'm going to hide a lot of functionality unless you ask for it.

PIRATE.H
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef PIRATE_H
#define PIRATE_H
#include "Sailor.h"

class Sailor;

class Pirate : public Sailor {
public:
	
     virtual bool update()
protected:

private:
	
};
#endif 



SAILOR.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
#ifndef SAILOR_H
#define SAILOR_H
#include "GameObject.h"
#include "Port.h"
#include "Dock.h"
#include "Model.h"
#include "Merchant.h"
#include "Pirate.h"

class Model;
/***********************************
* Sailor is the primary class of the
* game. Sailors are controlled by
* the user. A general overview is
* that the user tells the sailer 
* to travel, load/unload cargo, and 
* hide.
************************************/
class Sailor : public GameObject {
public:
virtual bool update()
protected:

private:
	
};

#endif 



I'll share a little more for GameObject

GAMEOBJECT.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
65
66
67
68
69
using namespace std;
#include <iostream>
#include "CartPoint.h"
#include "CartVector.h"

#ifndef GAME_OBJECT_H
#define GAME_OBJECT_H

/***********************************
* GameObject is the base class 
* sailor, dock, and port.
* Not a perfect definition, but a 
* gameObject is essentially an object
* that lives on the game grid
*
* Common charecteristics of all game
* objects live in this class. Aside
* from saving code, the reason we
* use a base class is so we can call
* virtual functions (like update)
* which simplifies our model.update()
* function.
************************************/
class GameObject {
public:
	GameObject(char in_code, int in_id);
	GameObject(char in_code, int in_id, CartPoint in_loc);
	virtual ~GameObject();

	// Displays information about a inherited class of gameObject
	virtual void show_status() = 0; 
	virtual bool is_alive();

	/***********************************
	* Update is one of the most
	* used functions in the game. For each
	* tick of the game clock, every 
	* gameObject calls it's update function.
	*
	* update does two things.
	* 
	* 1.) Corrects the states to match the
	* information about an object. I.E. if
	* a dock is empty, it should change
	* to state 'e' for empty.
	* 2.) It is the natural flow of the game.
	* If the user isn't giving specific orders,
	* update tells the gameObject what to do.
	* If Sailor is in loading state, it should
	* keep loading. If Sailor is full, it should
	* change states to say it's no longer loading.
	*
	* Furthermore, update notifies a state change
	* by returning true. Regardless of the type
	* of gameObject, if update returns true, we
	* expect the status to be different. Hence,
	* we should notify the user of new behavior.
	*
	* Sorry for the long explanation
	************************************/
	virtual bool update() = 0;

protected:

private:
	int id_num; // ID unique for each object of the same class
};

#endif 



Any comments appreciated. This isn't immediately due thank god.
Last edited on
circular dependecy on the includes
read point 4 http://www.cplusplus.com/forum/articles/10627/
Sailor.h should not need to include Pirate.h


Also, be careful with your memory management. It may be a lot better to use smart pointers.
Hey ne555, just to be clear, we should be very careful with our include files in our .h files. However, in our .cpps, can we go all out?
As a sidenote though. You were right. I was kinda just throwing around includes where I shouldn't have been
> in our .cpps, can we go all out?
yes, you should be able to include anything, as many times as you want and in any order.
GameObject doesn't have a default constructor but it looks like class Sailor assumes that it does. This might be the problem.

It looks like an object in doc_ptrs should always be in active_ptrs and object_ptrs. If that's true then you should override doc_ptrs::push_back() to enforce this:
1
2
3
4
5
void dock_ptrs::push_back(Dock *p) {
    list<Dock>::push_back(p);
    object_ptrs.push_back(p);
    active_ptrs.push_back(p);
}
Topic archived. No new replies allowed.