Jump to content

Why can't I use "<"

- - - - -

  • Please log in to reply
12 replies to this topic

#1
psepheroth

psepheroth

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
Hello, I'm a C++ newbie.
I tried the following:
	cout << "\n list start \n";


	list<int> l;

	

	l.push_back(5);

	l.push_back(4);

	l.push_back(3);

	l.push_back(2);

	l.push_back(1);


	list<int>::iterator i;


	for (i=l.begin(); i < l.end(); i++) 

	{

		cout << *i;

	}

But I got an error on the for loop. Seems like it cannot accept the "<" operator. I tried replacing it with "!=", and it can go well already.
Why is that?

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
I don't believe the iterator implementation has overloaded the < operator. (At least it sounds that way.)
(I could be wrong about this. I haven't used the STL libraries before. Somebody please correct me if I am.)
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
psepheroth

psepheroth

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
Yah, but when I try the code, it gives error.

#4
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
What's the error?

Edit: I looked up the iterator class for you. Seems the < operator is defined.
C++ Notes: STL: Iterator Operators
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#5
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
I just tried your code, and it seems to work fine. Did you add #include <list> ?

---------- Post added at 12:22 PM ---------- Previous post was at 12:16 PM ----------

another development. This does work, but has a warning. The reason that < gives this warning is that there is no overload for < in the list iterator class.

#6
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
If you have gcc 4.6 or greater, I'd recommend using ranged for:
for (auto& i : l)
    std::cout << i;

A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#7
psepheroth

psepheroth

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts

lespauled said:

I just tried your code, and it seems to work fine. Did you add #include <list> ?

---------- Post added at 12:22 PM ---------- Previous post was at 12:16 PM ----------

another development. This does work, but has a warning. The reason that < gives this warning is that there is no overload for < in the list iterator class.

Yes. I include.

---------- Post added at 09:00 AM ---------- Previous post was at 08:59 AM ----------

gregwarner said:

What's the error?

Edit: I looked up the iterator class for you. Seems the < operator is defined.
C++ Notes: STL: Iterator Operators

Here's the error:

Quote

myList.cpp: In function ‘int main()’:
myList.cpp:19: error: no match for ‘operator<’ in ‘li < l. std::list<_Tp, _Alloc>::end [with _Tp = int, _Alloc = std::allocator<int>]()’


#8
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
I get the same error on gcc 4.6.2. I'd say there's no need for < operator with iterators, since you're usually (all the time?) increment iterator by 1.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#9
psepheroth

psepheroth

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts

Flying Dutchman said:

I get the same error on gcc 4.6.2. I'd say there's no need for < operator with iterators, since you're usually (all the time?) increment iterator by 1.

But why not? Isn't "<" valid even if there is an increment iterator?

#10
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
As I pointed out on DaniWeb you have to use the != operator, the < operator is not defined.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#11
psepheroth

psepheroth

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts

Ancient Dragon said:

As I pointed out on DaniWeb you have to use the != operator, the < operator is not defined.

whereis it defined? Can we see a .h to indicate that the "<" is not defined? or a C++ reference perhaps?
My apology for being an amateur programmer :-)

#12
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts

psepheroth said:

whereis it defined? Can we see a .h to indicate that the "<" is not defined? or a C++ reference perhaps?
My apology for being an amateur programmer :-)

Look in <list> -- it declares all operators
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users