Sign in to follow this  

class declaration problem, some help?

This topic is 4590 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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 A
class A;

// Declare class B
class B;
{
A a;
};

// Declare class A
class A;
{
B b;
};

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)).

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
You have to pass through A the template parameters for B or explicitely pass parameters to B in A.

Please, as I said, try to read some examples and understand the code.

Good luck

Share this post


Link to post
Share on other sites

This topic is 4590 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this