Templates - What is wrong with this?

Hi there!

I was dealing with templates this time. Pretty neat concept, I have to admit. However, there is a problem: an error comes when I try to something like 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
namespace Bobby{
	/// -------------------
	/// @enum Item - types:
	/// -------------------
	enum ITEM{ITEM_COLLECTIBLE, ITEM_TOOL};
	/// ----------------------------------------------------------------
	/// @class Item<ITEM_COLLECTIBLE>:
	/// @brief Meant to handle collectibles (ammo, armor, health, etc.).
	/// ----------------------------------------------------------------
	template<int = ITEM_COLLECTIBLE>
	class Item{
		public:
			Item(void);
			Item(int);
			Item(Item&);
			~Item(void);
			Item& operator+=(int const);
			Item& operator+=(Item const&);
			Item& operator-=(int const);
			Item& operator-=(Item const&);
			Item& operator=(int const);
            Item& operator=(Item const&);
			bool operator==(int const) const;
			bool operator==(Item const&) const;
			bool operator!=(int const) const;
			bool operator!=(Item const&) const;
		protected:
			Item& fix(void);
			int miAmount, miAmountMax, miAmountNull;
	};
	/// ------------------------------------------------------------------------
	/// @class Item<ITEM_TOOL>:
	/// @brief Meant to handle tools & toolsets (weapons etc.).
	/// @note Extends the funtionality of template class Item<ITEM_COLLECTIBLE>.
	/// ------------------------------------------------------------------------
	template<>
	class Item<ITEM_TOOL> : public Item<ITEM_COLLECTIBLE>{
		public:
			template<int I> Item& setOwner(Bobby::Character<I>&);
	};
}

1
2
3
4
5
6
7
8
9
10
/// ----------------------------------------------
/// @brief Sets a character as an owner of "this".
/// @return Returns "this" with an owner.
/// ----------------------------------------------
template<int I>
template<int J>
Bobby::Item<I>& Bobby::Item<I>::setOwner(Bobby::Character<J>& character){
	moOwner = character;
	return *this;
}


NOTE: This is not the full code I have written. Code is cut due the limitations of amount of text.

So, this the error I get:
|202|error: no 'Bobby::Item<I>& Bobby::Item<<anonymous> >::setOwner(Bobby::Character<J>&)' member function declared in class 'Bobby::Item<<anonymous> >'|
As the error message says. In your template class Item there is no member function called setOwner().
closed account (zb0S216C)
Why have you declared Item twice (ambiguous)? That said, why does Item inherit itself?

Can you clarify what your code does? It's confusing me.

Wazzak
Last edited on
There is no point in using templates here. You have a small set of possible arguments and you intend to write a specialization for every one of them. Just use separate classes..
The basic idea behind this is that Item is a template class, which has been specialized in order to make it's use more flexible. For example, if I create an object using Bobby::Item<Bobby::ITEM_COLLECTIBLE> or Bobby::Item<Bobby::ITEM_TOOL>, they would be similiar kind of classes yet differenting crucially.

Character (which is defined somewhere else in my project codes) will use Bobby::Item<Bobby::ITEM_TOOL> (see my code above for more).

Why have you declared Item twice (ambiguous)? That said, why does Item inherit itself?


I can't understand what you mean. I have inherited Item<ITEM_COLLECTIBLE> from Item<ITEM_TOOL>.

The error comes right here:
1
2
3
template<int I>
template<int J>
Bobby::Item<I>& Bobby::Item<I>::setOwner(Bobby::Character<J>& character)


Why this doesn't work? Looks valid for me. Please help.
Last edited on
What part of that model is flexible?

About the error, I feel that it's there because you're not referring to the specialization, and the function doesn't exist in the normal template. Try something like
1
2
3
template<>
template<int J>
Bobby::Item<ITEM_TOOL>& Bobby::Item<ITEM_TOOL>::setOwner(Bobby::Character<J>& character)
I don't really know what the correct syntax should be..
What part of that model is flexible?

About the error, I feel that it's there because you're not referring to the specialization, and the function doesn't exist in the normal template. Try something like

1
2
3
template<>
template<int J>
Bobby::Item<ITEM_TOOL>& Bobby::Item<ITEM_TOOL>::setOwner(Bobby::Character<J>& character)


I don't really know what the correct syntax should be..


Wow, now there's one error: "Too many template-parameter lists." But guess how I fixed it? Like this:

1
2
template<int I>
Bobby::Item<Bobby::ITEM_TOOL>& Bobby::Item<Bobby::ITEM_TOOL>::setOwner(Bobby::Character<I>& character)


Hopefully this topic will help some people. Thanks for your help, it surely pleased me!
Topic archived. No new replies allowed.