class declaration problem, some help?

Started by
9 comments, last by xEricx 18 years, 11 months ago
here is my code:

//=========================================================================================================================================
// CustomList Class, contains all functions to edit the ListNode Class
//=========================================================================================================================================
	template< class cl >
	class CustomList
	{
	public:
		CustomList();
		CustomList( cl & el );
		~CustomList();
		void Insert_Front( cl & el );
		void Insert_End( cl & el );
		void Delete_Front();
		void Delete_End();
		void Delete_Value( cl el );
		bool IsIn( cl el );
		bool Is_Empty();
		int Size();
		Iterator< cl > Begin();
		void Print_List_Multi_Line();
		void Print_Back();
		void Print_List_Single_Line();
		void Print_List_Single_Line_Spaced();

	private:
		ListNode< cl >* _first;
		ListNode< cl >* _last;
	};
//=========================================================================================================================================
// Iterator Class, Gets the pointer to either the first node or the spot after the last node 
//=========================================================================================================================================
	template < class iter >
	class Iterator
	{
	public:
		Iterator();
		Iterator( int i, CustomList< iter > tmplist );
		~Iterator();
		Iterator< iter > operator ++();
		Iterator< iter > operator ++(int);
		Iterator< iter > operator --();
		Iterator< iter > operator --(int);



	private:
		Iterator< iter > _iterator;

	};

now as you can see I have 2 class: CustomList, Iterator. Know Iterator need to know about CustomList in order to the first of list point the last or first ListNode. now that is no problem here. CustomList needs to know about Iterator inorder to return the iterator to be able to use it is the program. that is where my problem is. Is there any way to a class to use another class the has not been declared yet but is in the same file? Do I need another file or something? is there any way this can be done?
Advertisement
You can pre-declare a name for this. I'm not sure how it would respond to use with templates but in general it works like this:
// Pre-declare name Aclass A;// Declare class Bclass B;{  A a;};// Declare class Aclass A;{  B b;};
Yup, it works even with the template, thanks. I knew you could do that with function but did not know I can do it on class too, nice to know.
My advise, when you're working on such problems, is to look what others have done.

If you open the list file for std::list in VC++, you'll see that their iterator is a class declared INSIDE the list, and that it never knows about the list itself, only the _Nodeptr which is typedef'd by _List_ptr based on the type of content and the allocator.

Hope this helps

Eric
Illco: That won't work unless you use pointers or references. As you've written it an instance of A contains an instance of B which contains an instance of A which contains an instance of B, ad infinitum.

The general rule is that you only need a full class definition if your class contains a member of that class or if you use a template on that class (depending on the nature of the template). In your case CustomList only needs a full class definition for ListNode. A forward declaration is sufficient for Iterator. Equally Iterator only needs a forward declaration of CustomList.

//=========================================================================================================================================// CustomList Class, contains all functions to edit the ListNode Class//=========================================================================================================================================	template < class cl >	class Iterator< cl >;	template< class cl >	class CustomList	{	public:		CustomList();		CustomList( cl & el );		~CustomList();		void Insert_Front( cl & el );		void Insert_End( cl & el );		void Delete_Front();		void Delete_End();		void Delete_Value( cl el );		bool IsIn( cl el );		bool Is_Empty();		int Size();		Iterator< cl > Begin();		void Print_List_Multi_Line();		void Print_Back();		void Print_List_Single_Line();		void Print_List_Single_Line_Spaced();	private:		ListNode< cl >* _first;		ListNode< cl >* _last;	};//=========================================================================================================================================// Iterator Class, Gets the pointer to either the first node or the spot after the last node //=========================================================================================================================================	template < class iter >	class Iterator	{	public:		Iterator();		Iterator( int i, CustomList< iter > tmplist );		~Iterator();		Iterator< iter > operator ++();		Iterator< iter > operator ++(int);		Iterator< iter > operator --();		Iterator< iter > operator --(int);	private:		boost::shared_pointer< Iterator< iter > > _iterator;	};

You might want to change that shared_pointer depending on what _iterator actually is.

Alternatively just use std::list.

Enigma
Also, you might wanna change the constructor of your iterator which takes a COPY of a list as a parameter, make it a const &, there's no reason your ctor should change the list itself.

You also have other cases where refs / const refs should be useful to avoid copies of objects.
Quote:
xEric:/b>Illco: That won't work unless you use pointers or references. As you've written it an instance of A contains an instance of B which contains an instance of A which contains an instance of B, ad infinitum.

Yes I know but I just wanted to illustrate the concept ('do something with A' (but not anything that will require a definition of A, true)).
if i declare a class inside of a class do it use the same template as the class it is in or do i have to declare a new template.
Quote:Original post by 3dmodelerguy
if i declare a class inside of a class do it use the same template as the class it is in or do i have to declare a new template.


Not quite sure if this is what your asking, but in this code:
template <typename T>class A{public:class B{};};

Then yes, each type of A< T > contains a type A< T >::B which is of course different from each other type of A< T >::B

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

no, I mean this:

template < class a >
class A
{
public:
template< class b >
class B
{
};
};

or does B have to be the same as A?

This topic is closed to new replies.

Advertisement