Click here to Skip to main content
15,178,370 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
EDIT: In bold, I've added changes. These changes helped but the one thing that's still giving me frustration is this new found error. Cannot convert Node to Node * in while(__pred(*__first)) and the same error again Node to Node * in while(__pred(*__next)).

Hopefully this isn't a long one at all but it looks like I'm going to need a lot of details of what I'm doing. I'll start first with my problem. I'm basically making a class, it's pretty simple, it only has two attributes. One is a mutable std::string for name_ and the other is a mutable bool for gender_. What I'm trying to do with this class is utilize it for std::partition from the algorithms library. Basically all females of the container will be moved forward while the guys move back. I'm basically a predicate function like this.

std::partition(starting_iterator, ending_iterator, is_female).

The only problem is that my iterator just simply is not compatible for the STL.

To do this I have a Node class for a Singly Linked list. Then with that Singly Linked List, I created my own iterators. I'll start with showing everyone my implementation of my People class. Then I'll move on to my Node, and then to my Singly Linked List class that houses my version of an iterator.

C++
//The people class. bool true for guys, bool false for female
class People
{
private:
mutable std::string name_;
mutable bool gender_;
public:
People(std::string name, bool gender)
: name_(name), gender_(gender)
{

}
std::string get_name() const
{
return name_;
}
bool get_gender() const
{
return gender_;
}

//Now for my node itself for the singly linked list down below.
#include <cstddef>
class Node
{
private:
int key_;
Node * next_;
public:
Node(int key, Node * next = nullptr)
: key_(key), next_(next)
{
}
int get_key() const
{
return key_;
}
Node * get_node() const
{
return next_;
}
void insert_next(Node * next)
{
next_ = next;
}
};
std::ostream & operator<< (std::ostream & cout, const Node & node)
{
cout << &node << ' ' << node.get_key() << ' ' << node.get_next();
return cout;
}

// Now my for Single Linked List. Key note is that, for now, I'm leaving destructors and copy constructors. I'm focusing on addressing my iterator for this whole thing.
#include "Node.h"
class SLL
{
friend class iterator;
private:
Node * root_;
public:
SLL()
: root_(nullptr)
{
}
class iterator<std::iterator<std::forward_iterator_tag, Node *> // this is housed inside the SLL class......
{
friend SLL;
private:
mutable Node * iterator_;
public:
iterator()
: iterator_(nullptr)
{

}
iterator(Node * node)
: iterator_(node)
{

}
bool operator !=(const iterator & amp)
{
return iterator_ != amp.iterator_;
}
bool operator ==(const iterator & amp)
{
return iterator_ == amp.iterator_;
}
iterator & operator++() 
{
iterator_ = iterator_->get_next();
return *this;
]
iterator operator++(int) 
{
iterator temp(*this);
iterator_ = iterator_->get_next();
return temp;
}
Node & operator*() const
{
return *iterator_;
}
bool get_gender() const
{
return iterator_->get_gender();
}
};
iterator begin() const
{
return iterator(root_);
}
iterator end() const
{
return iterator();
}
void insert(const People & people)
{
if (!root_)
{
root_ = new Node(people.get_name(), people.get_gender());
}
else
{
Node * temp = root_;
while (temp->get_next())
{
temp = temp->get_next();
}
temp->insert_next(new Node(people.get_name(), people.get_gender()));
}
};


And there's the whole thing.
My main looks something like this.

C++
bool is_female(SLL::iterator & iterator)
{
if (iterator.get_gender() == 0) return 1;
else return 0;
}

int main()
{
People Peter("Peter", 1);
People Martha("Martha, 0):
SLL sl;
sl.insert(Peter);
sl.insert(Martha);
std::partition(sl.begin(), sl.end(), is_female);
return 0;
}


Doesn't work for the std::partition. It's saying something about std::iterator__iterator_category(__first) and something else about not function for call to __iterator_category(SLL::iterator &);

What is exactly going on to make this whole thing happen? What am I missing in my implementation of my iterator?

What I have tried:

I'm absolutely left without much of an answer on my own. This is pretty new to me and I really didn't think I was going to encounter this but it's here. Time to figure it out.
Posted
Updated 7-Feb-21 15:59pm
v3

The predicate function should take a Node& (or const Node&) as argument.

Try something like this:
C++
bool is_female(const Node& n)
{
if (n.get_gender() == 0) return 1;
else return 0;
}


The std::partition algorithm applies the indirection operator on the iterator. See the "Possible Implementation" section of [^]
   
Quote:
I'm absolutely left without much of an answer on my own. This is pretty new to me and I really didn't think I was going to encounter this but it's here. Time to figure it out.

Time to ask your code to show you what is going on, the tool to use is the debugger.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900