std::remove_if errors!

C

CupOfWater

Hi, I'm getting some errors using remove_if that I can not fix at all.
Let me paste my code and also the error here. I went on IRC and
people were mostly no help. The compiler is g++ 3.2.3. Thanks in
advance.

/usr/include/c++/3.2.3/bits/stl_algo.h: In function `_OutputIter
std::remove_copy_if(_InputIter, _InputIter, _OutputIter,
_Predicate) [with
_InputIter = std::_Rb_tree_iterator<std::pair<Algo489::node_t,
Algo489::node_t>, const std::pair<Algo489::node_t,
Algo489::node_t>&, const
std::pair<Algo489::node_t, Algo489::node_t>*>, _OutputIter =
std::_Rb_tree_iterator<std::pair<Algo489::node_t, Algo489::node_t>,
const
std::pair<Algo489::node_t, Algo489::node_t>&, const
std::pair<Algo489::node_t, Algo489::node_t>*>, _Predicate =
Algo489::Graph::equal_edge_cmp]':
/usr/include/c++/3.2.3/bits/stl_algo.h:1085: instantiated from
`_ForwardIter std::remove_if(_ForwardIter, _ForwardIter, _Predicate)
[with _ForwardIter = std::_Rb_tree_iterator<std::pair<Algo489::node_t,
Algo489::node_t>, const std::pair<Algo489::node_t, Algo489::node_t>&,
const std::pair<Algo489::node_t, Algo489::node_t>*>, _Predicate =
Algo489::Graph::equal_edge_cmp]'
algo.h:116: instantiated from here
/usr/include/c++/3.2.3/bits/stl_algo.h:1017: passing `const
std::pair<Algo489::node_t, Algo489::node_t>' as `this' argument of
`
std::pair<Algo489::node_t, Algo489::node_t>&
std::pair<Algo489::node_t,
Algo489::node_t>::eek:perator=(const std::pair<Algo489::node_t,
Algo489::node_t>&)' discards qualifiers



// Header


#ifndef __ALGO489__H_
#define __ALGO489__H_
#include <netinet/in.h>
#include <sys/types.h>
#include <iostream>
#include <iterator>
#include <vector>
#include <functional>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
namespace Algo489 {
struct node_t {
node_t(){}
node_t(std::string ip, u_short port, char type):ip_(ip),
port_(port), type_(type) {}
std::string ip_;
u_short port_;
char type_;
};
bool operator==(const node_t& lhs, const node_t& rhs);
struct Graph {
typedef std::pair<node_t, node_t> edge_t;
struct less_node_cmp : public std::binary_function<node_t, node_t,
bool> {
bool operator() (const node_t& f, const node_t& s) const
{
if(f==s)
return false;
else {
return strcmp(f.ip_.c_str(), s.ip_.c_str());
}
}
};
struct equal_edge_cmp {
equal_edge_cmp(const edge_t& e) : edge(e) {}
bool operator() (const edge_t& e) const
{
return ((e.first == edge.first && e.second == edge.second) ||
(e.first == edge.second && e.second == edge.first));
}
edge_t edge;
};
struct less_edge_cmp : public std::binary_function<node_t, node_t,
bool> {
bool operator() (const edge_t& f, const edge_t& s) const
{
bool lessthan;
equal_edge_cmp e(f);
if(e(s)) {
return false;
}
else {
return strcmp(f.first.ip_.c_str(), s.first.ip_.c_str());
}
}
};
struct print_node {
void operator() (const node_t& e) const
{
std::cout << e.ip_ << " " << e.port_ << " " << e.type_ <<
std::endl;
}
};
struct print_conn {
void operator() (const edge_t& e) const
{
std::cout << e.first.ip_ << " " << e.first.port_ << " " <<
e.first.type_;
std::cout << " connected to: ";
std::cout << e.second.ip_ << " " << e.second.port_ << " " <<
e.second.type_;
std::cout << std::endl;
}
};
typedef std::set<node_t, less_node_cmp> nodes_t;
typedef std::set<edge_t, less_edge_cmp> vertices_t;
// functions
node_t addentry(const node_t& e)
{
return *nodes_.insert(e).first;
}
virtual bool rmentry(const node_t& e)
{
for(Graph::nodes_t::iterator i = nodes_.begin(); i != nodes_.end();
++i) {
if(*i == e) {
print_node p;
std::cout << "removing node: "; p(*i);
nodes_.erase(i);
return true;
}
}
return false;
}
// remove the links that contain that node
/*
Graph::vertices_t::iterator i = links_.begin();
while(i != links_.end()) {
Graph::edge_t tmp = *i++;
Graph::disconnect(tmp.first, e);
Graph::disconnect(tmp.second, e);
}
*/
virtual bool connect(const node_t& f, const node_t& s)
{
bool found1=false, found2=false;
for(Graph::nodes_t::iterator i = nodes_.begin(); i != nodes_.end();
++i) {
if(*i == f) found1=true;
if(*i == s) found2=true;
if(found1 && found2) {
print_node p;
links_.insert(std::make_pair(f,s));
return true;
}
}
std::cout << "one or more nodes are not found\n";
return false;
}
virtual bool disconnect(const node_t& f, const node_t& s)
{
edge_t tmp = std::make_pair(f,s);
vertices_t::iterator end = std::remove_if(links_.begin(),
links_.end(), equal_edge_cmp(tmp));
if(end == links_.end())
return false;
links_.erase(end, links_.end());
return true;
}
virtual void print() const
{
for_each(nodes_.begin(), nodes_.end(), print_node());
std::cout << "********************************************************************************\n";
for_each(links_.begin(), links_.end(), print_conn());
std::cout << "********************************************************************************\n";
}
protected:
nodes_t nodes_;
vertices_t links_;
};
void algotest();
}
#endif




//CPP


#include "algo.h"
namespace Algo489 {
bool operator==(const node_t& lhs, const node_t& rhs)
{
return !strcmp(lhs.ip_.c_str(), rhs.ip_.c_str()) && lhs.port_ ==
rhs.port_ && lhs.type_ == rhs.type_;
}
void algotest()
{
Graph db;
std::vector<node_t> incoming;
incoming.push_back(node_t("141.213.12.4", 21448, 'r'));
incoming.push_back(node_t("141.212.12.4", 2447, 'h'));
incoming.push_back(node_t("141.211.12.4", 1448, 'r'));
incoming.push_back(node_t("141.210.12.4", 148, 'h'));
incoming.push_back(node_t("141.209.12.4", 21448, 'h'));
incoming.push_back(node_t("141.208.12.4", 1445, 'r'));
incoming.push_back(node_t("141.207.12.4", 21444, 'h'));
incoming.push_back(node_t("141.206.12.4", 1445, 'h'));
incoming.push_back(node_t("141.205.12.4", 2445, 'r'));
incoming.push_back(node_t("141.204.12.4", 438, 'r'));
incoming.push_back(node_t("141.203.12.4", 21449, 'r'));
for(std::vector<node_t>::iterator i = incoming.begin();
i!=incoming.end(); ++i) {
db.addentry(*i);
}
db.print();
std::cout << "**********************************************\n";
db.connect(
node_t("141.203.12.4", 21449, 'r'),
node_t("121.203.12.4", 21449, 'r')
);
db.connect(
incoming[0],
incoming[1]
);
db.connect(
incoming[1],
incoming[0]
);
db.connect(
incoming[0],
incoming[1]
);
db.connect(
incoming[2],
incoming[4]
);
db.connect(
incoming[6],
incoming[7]
);
db.print();
db.disconnect(
incoming[1],
incoming[0]
);
db.print();
//db.connect(e2, e);
db.rmentry(incoming[0]);
std::cout << "after remove" << std::endl;
db.print();
}
}
 
D

David B. Held

CupOfWater said:
Hi, I'm getting some errors using remove_if that I can not fix at
all. Let me paste my code and also the error here.

That's a good start, but it helps if you give some clues, like
describing where you suspect the error is based on what
you have tried and what you have looked at.
I went on IRC and people were mostly no help.

Probably because nobody wants to wade through hundreds
of lines of someone else's code.
The compiler is g++ 3.2.3.
[...]

The first thing you can do is upgrade to 3.3.x. If you are
still having problems, try again.

Dave
 
R

Rob Williscroft

CupOfWater wrote in
Hi, I'm getting some errors using remove_if that I can not fix at all.
Let me paste my code and also the error here. I went on IRC and
people were mostly no help. The compiler is g++ 3.2.3. Thanks in
advance.

std::remove_if isn't the right way to remove from a std::set<>, the
algorithm is designed for sequences such as std::vector, deque and list.

Create your own set_remove_if function use std::find_if() to find the
elements you want to erase then call your_set.erase( iterator ) to
remove them.

HTH

Rob.
 
S

Stephen M. Webb

Hi, I'm getting some errors using remove_if that I can not fix at all.
Let me paste my code and also the error here. I went on IRC and
people were mostly no help. The compiler is g++ 3.2.3. Thanks in
advance.

There's too much code for me to wade through, you should try to find
the simplest test case possible that reproduces the bug (often in the
process of trying to do so, you will find the source of the bug).

The first thing that comes to mind here is that you're trying to use
remove_if on a std::set. There is (was) a problem with this, and I
believe there was a fix in later versions of the g++ standard library
implementation.
 
J

Jerry Coffin

Hi, I'm getting some errors using remove_if that I can not fix at all.
Let me paste my code and also the error here. I went on IRC and
people were mostly no help. The compiler is g++ 3.2.3. Thanks in
advance.

First, try to reduce the code to the minimum possible that demonstrates
the problem. In the process, be _sure_ you eliminate the use of non-
standard headers (e.g. netinet/in.h).

In that process, there's a pretty fair chance that you'll figure out
what's wrong on your own, but if you're still having the same problem,
post the what you get, and there's a much better chance of somebody
figuring out the problem.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top