Return class record std::list

X

XX

Please help, and thanks.....
How to compare iterator with class [s==skip_share].
Return iterator as a class record [return s;].

error C2678: binary '==' : no operator found which takes a left-hand operand
of type 'std::list<_Ty>::_Iterator<_Secure_validation>' (or there is no
acceptable conversion)
error C2440: 'return' : cannot convert from
'std::list<_Ty>::_Iterator<_Secure_validation>' to 'cShare *'

#include <list>
using namespace std;


class cShare {
private:
int connection_id;
public:
cShare(int _connection_id);
int Get_connection_id(void) const { return connection_id; };
};

cShare::cShare(int _connection_id)
{
connection_id = _connection_id;
}


class cShares
{
private:
list<cShare> cshares;
public:
cShares();
virtual ~cShares();
cShare *cShares::SearchCon(int connection_id, cShare *skip_share);
};

cShares::cShares()
{
}

cShares::~cShares()
{
}

cShare *cShares::SearchCon(int connection_id, cShare *skip_share)
{
list<cShare>::const_iterator s;
for (s = cshares.begin(); s != cshares.end(); ++s) {
if(s==skip_share) continue;
if(s->Get_connection_id()==connection_id) return s;
}
return NULL;
}
 
I

Ian Collins

XX said:
Please help, and thanks.....
How to compare iterator with class [s==skip_share].
Return iterator as a class record [return s;].

error C2678: binary '==' : no operator found which takes a left-hand operand
of type 'std::list<_Ty>::_Iterator<_Secure_validation>' (or there is no
acceptable conversion)
error C2440: 'return' : cannot convert from
'std::list<_Ty>::_Iterator<_Secure_validation>' to 'cShare *'

#include <list>
using namespace std;


class cShare {
private:
int connection_id;
public:
cShare(int _connection_id);
int Get_connection_id(void) const { return connection_id; };
};

cShare::cShare(int _connection_id)
{
connection_id = _connection_id;
}


class cShares
{
private:
list<cShare> cshares;
public:
cShares();
virtual ~cShares();
cShare *cShares::SearchCon(int connection_id, cShare *skip_share);
};

cShares::cShares()
{
}

cShares::~cShares()
{
}

cShare *cShares::SearchCon(int connection_id, cShare *skip_share)

Why not make life a little easier and return an iterator?

It's probably clearer to pass skip_share by const reference, you aren't
changing its value:

list<cShare>::const_iterator
cShares::SearchCon(int connection_id, const cShare& skip_share)
{
for (list<cShare>::const_iterator s = cshares.begin();
s != cshares.end(); ++s)
{
if(*s==skip_share)
continue;
if(s->Get_connection_id()==connection_id)
return s;
}
return cshares.end();
}

You will still have to add a comparison operator to cShare.
 
A

Alf P. Steinbach

* XX:
Please help, and thanks.....
How to compare iterator with class [s==skip_share].
Return iterator as a class record [return s;].

error C2678: binary '==' : no operator found which takes a left-hand operand
of type 'std::list<_Ty>::_Iterator<_Secure_validation>' (or there is no
acceptable conversion)
error C2440: 'return' : cannot convert from
'std::list<_Ty>::_Iterator<_Secure_validation>' to 'cShare *'

#include <list>
using namespace std;


class cShare {
private:
int connection_id;
public:
cShare(int _connection_id);
int Get_connection_id(void) const { return connection_id; };
};

You're using a lot of prefixes, "c...", "_...", "Get_". They serve no useful
purpose and reduce readability. On the other hand, a prefix on the data member
name would be useful, but is lacking.

cShare::cShare(int _connection_id)
{
connection_id = _connection_id;
}

When there's no special reason to use assignment, consider a memory initialiser
list:

cShare::cShare( int id )
: connection_id( id )
{}

class cShares
{
private:
list<cShare> cshares;
public:
cShares();
virtual ~cShares();
cShare *cShares::SearchCon(int connection_id, cShare *skip_share);
};

For this functionality, if cShare in actuality holds more information than just
an id then you'd be better off using a std::map.

If it just holds an id then the functionality amounts to a std::set.

cShares::cShares()
{
}

cShares::~cShares()
{
}

cShare *cShares::SearchCon(int connection_id, cShare *skip_share)
{
list<cShare>::const_iterator s;
for (s = cshares.begin(); s != cshares.end(); ++s) {
if(s==skip_share) continue;

A list iterator is not a pointer to a list element.

You might check the id:

if(
skip_share != 0 &&
s->Get_connection_id() == skip_share->Get_connection_id()
)

If skip_share can't be 0 then you should pass it by reference, not as a pointer.

if(s->Get_connection_id()==connection_id) return s;

Ditto: 's' is a list iterator, not a pointer to cShare.

You can do

return &*s;

}
return NULL;
}


tShares!, & hth.,

- Alf
 
X

XX

XX said:
Please help, and thanks.....
How to compare iterator with class [s==skip_share].
Return iterator as a class record [return s;].

error C2678: binary '==' : no operator found which takes a left-hand
operand of type 'std::list<_Ty>::_Iterator<_Secure_validation>' (or there
is no acceptable conversion)
error C2440: 'return' : cannot convert from
'std::list<_Ty>::_Iterator<_Secure_validation>' to 'cShare *'

#include <list>
using namespace std;


class cShare {
private:
int connection_id;
public:
cShare(int _connection_id);
int Get_connection_id(void) const { return connection_id; };
};

cShare::cShare(int _connection_id)
{
connection_id = _connection_id;
}


class cShares
{
private:
list<cShare> cshares;
public:
cShares();
virtual ~cShares();
cShare *cShares::SearchCon(int connection_id, cShare *skip_share);
};

cShares::cShares()
{
}

cShares::~cShares()
{
}

cShare *cShares::SearchCon(int connection_id, cShare *skip_share)
{
list<cShare>::const_iterator s;
for (s = cshares.begin(); s != cshares.end(); ++s) {
if(s==skip_share) continue;
if(s->Get_connection_id()==connection_id) return s;
}
return NULL;
}

Thanks Ian and Alf.
Now store pointers instead of objects, preventing lot of casting.
It's a lot bigger application, need a lot of rewriting when make everything
std:list.
Greetings and thanks again,
Appie

class cShare {
private:
int connection_id;
public:
bool operator==(const cShare &s) const;
cShare(int _connection_id);
int Get_connection_id(void) const { return connection_id; };
};

bool cShare::eek:perator==(const cShare &s) const
{
if(this->connection_id != s.connection_id) return false;
return true;
}

cShare::cShare(int _connection_id)
{
connection_id = _connection_id;
}


class cShares
{
private:
list<cShare *> cshares;
public:
cShares();
virtual ~cShares();
cShare *cShares::SearchCon(int connection_id, cShare *skip_share);
};

cShares::cShares()
{
}

cShares::~cShares()
{
}

cShare *cShares::SearchCon(int connection_id, cShare *skip_share)
{
list<cShare *>::const_iterator s;
for (s = cshares.begin(); s != cshares.end(); ++s) {
if(*s==skip_share) continue;
if((*s)->Get_connection_id()==connection_id) return *s;
}
return NULL;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top