I'm having a problem with a homework assignment. I had to create a doubly-linked List class with an iterator. I have done this except there is one "display" method that displays a large integer that is stored in this List class. The method is constant and my custom Iterator class will not work with it. Can someone tell me a short-cut to fix this problem?

Here is the display method:

//--- Definition of display()
void BigInt::display(ostream & out) const
{ 
	int blockCount = 0;
	const int BLOCKS_PER_LINE = 20;   // number of blocks to display per line


	for (Iter<short int> it(myList.begin()); ; )
	{
		out << setfill('0'); 
		if (blockCount == 0)
			out << setfill(' '); 

		if (it == myList.end())
			return;

		out << setw(3) << *it;
		blockCount++ ;

		it++;
		if (it != myList.end())
		{
			out << ',';
			if (blockCount > 0 && blockCount % BLOCKS_PER_LINE == 0)
				out << endl;
		}
	}
}

That display method is called by this operation method:

inline ostream & operator<<(ostream & out, const BigInt & number)
{
	number.display(out);
	return out;
}

Is there any way to go through this list without creating a whole new constant Iterator class?

Recommended Answers

All 4 Replies

Have you tried the method without the setfill() and setw() stream manipulators? Since your method is const, they may not be allowed because you are changing the stream.

Have you tried the method without the setfill() and setw() stream manipulators? Since your method is const, they may not be allowed because you are changing the stream.

No, but they were used before the code was modified with my custom Iterator and List classes without a problem, so I don't think that is a problem. The error I get is

[error C2662: 'List<T>::begin' : cannot convert 'this' pointer from 'const List<T>' to 'List<T> &']

That error says it all. Add const to the definition of your "begin()" method. I think there is more to your issue though.

That error says it all. Add const to the definition of your "begin()" method. I think there is more to your issue though.

Iter<T>  begin()
	{
		return Iter<T>(this,head->suc);//
	}

	Iter<T>  end()     
	{ 
		return Iter<T>( this,head ); 
	}

Here is the code for the begin() and end() functions. I tried adding const to the functions and I get this error:

[error C2665: 'Iter<T>::Iter' : none of the 3 overloads could convert all the argument types]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.