LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-18-2004, 11:50 PM   #1
ashwinipahuja
Member
 
Registered: Mar 2004
Posts: 79

Rep: Reputation: 15
Lightbulb _gnu Iterator Error-g++


Following piece of code is giving error under g++

#include<iostream>
#include<vector>
using namespace std;
class xyz
{
public:

};
int main()
{
vector<xyz *>::iterator *p;
vector<xyz *>::iterator *x=NULL;
p= x;
if( p && *p)
{
cout<<"P";
}
}


The error message is
gnu.cpp: In function `int main()':
gnu.cpp:14: no match for `__gnu_cxx::__normal_iterator<xyz**, std::vector<xyz*,
std::allocator<xyz*> > >*& && __gnu_cxx::__normal_iterator<xyz**,
std::vector<xyz*, std::allocator<xyz*> > >&' operator
gnu.cpp:14: candidates are: operator&&(bool, bool) <built-in>
gnu.cpp:18:2: warning: no newline at end of file


If anyone has some idea then please help
Thanx in advance
Ashwini
 
Old 05-19-2004, 07:05 AM   #2
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
As the compiler says, you are trying to perform this operation.

__gnu_cxx::__normal_iterator<xyz**, std::vector<xyz*,std::allocator<xyz*> > >*&
&&
__gnu_cxx::__normal_iterator<xyz**,std::vector<xyz*, std::allocator<xyz*> > >&

and the only available one is:
gnu.cpp:14: candidates are: operator&&(bool, bool) <built-in>

How do you propose to convert from an iterator to bool?
Code:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
        vector<int>::iterator p;
        if (&p) cout << "Pointer OK\n";
        if (p)  cout << "Compiler will balk here - can't convert iter to bool\n";
        if (&p && p) cout << "Compiler will balk here, too, with the error above\n"
}
not to mention the fact that you are dereferencing a NULL pointer (through *p) which will result in undefined behaviour if at runtime you actually got to the bool(*p) bit.

You could get around this through operator overloading (which will be both error prone and most likely needlessly complex), however I have to ask why you are using pointers to iterators - what is wrong with just using an iterator?

Last edited by dakensta; 05-19-2004 at 07:08 AM.
 
Old 05-26-2004, 12:57 AM   #3
ashwinipahuja
Member
 
Registered: Mar 2004
Posts: 79

Original Poster
Rep: Reputation: 15
Hi dakensta,
But i dont want to mess up with the source code,
So i am looking for a solution in the existing code....
wut changes will i have to make.
 
Old 05-26-2004, 08:14 AM   #4
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Every time you try and dereference p you must check it to be not null.

This is why you have the line (sorry, I missed this before, tut tut) here, but every check must be preceded by this:

if (p && *p)

Secondly you need to decide what makes your iterator valid. I can't answer this, because I don't know the context.

Maybe you could post a description of what you would like *p to give as true,

I would guess that you want to do is check that the iterator is in a certain container, in which case you are going to have to change either the iterator class (maybe derive from container::iterator) and add some means or add a contianer to the iterator validity check. Iterators don't know the container from which they reference objects.

Both will involve changes to the source code.

Sorry, I am a bit busy at the moment, I'll post back when I have had a chance to think about this a bit more.
 
Old 05-26-2004, 11:17 PM   #5
ashwinipahuja
Member
 
Registered: Mar 2004
Posts: 79

Original Poster
Rep: Reputation: 15
Ok dakensta, please let me know wut changes have to be made to the source code, whenever u get some time
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert between list<T>::iterator and T* shreks Programming 2 08-26-2004 05:27 PM
Stl iterator woes Config Programming 4 06-23-2004 05:31 PM
compile error while returning STL iterator as object * rameshmiraje Programming 2 06-20-2004 01:50 PM
error in assigning the iterator to refrence varaible ashwinipahuja Programming 1 06-18-2004 05:55 AM
Problem in Iterator- g++ ashwinipahuja Programming 1 04-30-2004 12:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration