While trying to create a Binary Search Tree (BST) using C++11 I hit a snag.
I can't extract a properly created BST out of the function where it is created.
The function reads data from a file (in this case just a number on each line) and builds a BST from those. The building part in the loop works correctly.
The problem is I can't get the temporary object moved where I want it to be.
Zooming in on the problem I think, see edit 3.
More context:
BST<T> is a class that derives publicly from std::unique_ptr<BSTknoop<T>>
BST<T> also inherits the constructors of unique_ptr<BSTknoop<T>> with
template<class T>
using BSTknoopptr=std::unique_ptr<BSTknoop<T>>; // alias template
and
using BSTknoopptr<T>::BSTknoopptr; // in the body of the BST<T> class declaration`
A BSTknoop<T> is a data structure with 3 fields
one T object to hold the node data
two BST<T> objects left and right (each child is a tree in its own right)
the goal is to move my newly created BST into the calling object. The idea is that in main you can call BST<int> tree; tree.lees(ifs); (with ifs an open input filestream) and tree holds the properly filled BST afterwards.
The function:
template<class T>
istream& BST<T>::lees(istream& is){
string lijn;
T knoopwaarde;
getline(is,lijn);
knoopwaarde = stoi(lijn);
BST<T> temptree(new BSTknoop<T>());
temptree->sl = knoopwaarde;
for(int i=1; i<DATA_SET_LENGTH ; i++){
getline(is,lijn);
knoopwaarde=stoi(lijn);
temptree.add(knoopwaarde);
cout<<temptree;
}
this->swap(temptree); /* This does not work */
/* *this = move(temptree); this does not work either*/
return is;
}
I have also tried to return a BST<T> from the function and moving that result in the main. That didn't work either.
The program crashes at runtime with an unknown signal.
Side note: I'm unsure as to why BST<T> temptree(new BSTknoop<T>()); works in regard to the templating. The construction works because BST<T> inherits the constructors for unique_ptr<BSTknoop<T>>
edit 1: the declaration of the BST<T> class:
template <class T>
class BST:public BSTknoopptr<T>{
using BSTknoopptr<T>::BSTknoopptr;
public:
friend istream& operator>>(istream& is, BST<T>& bb){
return bb.lees(is);
}
friend ostream& operator<<(ostream& os, const BST<T>& bb){
//return bb.schrijflevelorder(os);
return bb.schrijfKnoop(os);
}
void add(const T&);
ostream& schrijf(ostream&);
ostream& schrijfKnoop(ostream&) const;
int aantalSleutels() const;
istream& lees(istream&);
ostream& schrijflevelorder(ostream& os) const;
private:
};
the declaration of theBSTknoop<T> class:
template <class T>
class BSTknoop{
friend class BST<T>;
public:
BSTknoop() {}
explicit BSTknoop(T _sl) : sl(_sl) {}
private:
T sl;
BST<T> links,rechts;
};
edit 2: I've written a move constructor and move assignment operator. I've made sure to retain the default constructor as well using BST<T>()=default; I'm confused though: the BST class doesn't have any members for which I would have to implement my own move constr / operator.
However, BST has inherited from unique_ptr<BSTknoop<T>>so implicitly, it must hold a member of that type. Suppose I want to keep the inheritance, is there any neat way to make this work?
Is is also true that I can't (or perhaps shouldn't) implement a copy constr / operator as those are deleted for unique_ptr?
template<class T>
BST<T>::BST(BST<T>&& other){
*this = move(other);
}
template<class T>
BST<T>& BST<T>::operator=(BST<T>&& other){
cout<<"called move operator BST"<<endl;
(*this).BSTknoopptr<T>::operator=(move(other));
return *this;
}
edit 3: with my own move constr / operator, the temptree no longer gets filled properly. Below is my code for add which is used to build the tree. If I omit my own move constr / operator, then this works. What I need is an operation that works like this->get()->links = move(tmp).
types this->get()->links => BST<T>
tmp => BST<T>
How come this works? It does the operation I need, why can't I use something similar to do *this = move(temptree) by writing the operation myself.
template<class T>
void BST<T>::add(const T& value){
if(value <= this->get()->sl){
if(this->get()->links != nullptr){
this->get()->links.add(value);
}
else {
BST<T> tmp(new BSTknoop<T>(value));
this->get()->links = move(tmp);
}
}
else{
if(this->get()->rechts != nullptr){
this->get()->rechts.add(value);
}
else{
BST<T> tmp(new BSTknoop<T>(value));
this->get()->rechts = move(tmp);
}
}
}
Okay, I made the stupidest mistake. There is nothing wrong with the inheritance and accompanying move semantics. I used the DATA_SET_LENGTH preprocessor directive inside my istream& BST<T>::lees(istream& is) function. While my final dataset will hold a million items. My dataset for testing purposes only holds 12. I forgot to change the value of DATA_SET_LENGTH so stoi(lijn) crashed.
For anyone interested in the solution:
notes
You don't need to write much code to make this work.
It is possible to write BST<T>& operator=(BSTknoopptr<T>&&); yourself and it works. However even without explicitly writing your own it also works.
The BST class has no fields of pointer type so we don't get into trouble with unique_ptr not having a virtual destructor
Final question (perhaps someone can comment):
The inherited operator operator=(BSTknoopptr<T>&&), what does its signature look like in BST<T>? Mostly in regards to return type (if it's now a member of BST<T> what type does it return?).
** TLDR: why can I rely on the inherited move operator / move constructors of unique_ptr<BSTknoop<T>>? **
bst.h
#define DATA_SET_LENGTH 12
using namespace std;
template <class T>
class BST;
template <class T>
class BSTknoop;
template<class T>
using BSTknoopptr=std::unique_ptr<BSTknoop<T>>;
template <class T>
class BST:public BSTknoopptr<T>{
using BSTknoopptr<T>::BSTknoopptr;
public:
BST<T>& operator=(BST<T>&&) = default;
BST<T>(BST<T>&&) = default;
BST<T>()=default;
//BST<T>& operator=(BSTknoopptr<T>&&);
friend istream& operator>>(istream& is, BST<T>& bb){
return bb.lees(is);
}
friend ostream& operator<<(ostream& os, const BST<T>& bb){
//return bb.schrijflevelorder(os);
return bb.schrijfKnoop(os);
}
void add(const T&);
//schrijf schrijft uit in een vorm die min of meer menselijk leesbaar is
ostream& schrijf(ostream&);
ostream& schrijfKnoop(ostream&) const;
int aantalSleutels() const;
istream& lees(istream&);
//schrijflevelorder schrijft uit in een vorm die door lees(...) kan gelezen worden.
ostream& schrijflevelorder(ostream& os) const;
private:
};
template <class T>
class BSTknoop{
friend class BST<T>;
public:
BSTknoop() {}
explicit BSTknoop(T _sl) : sl(_sl) {}
private:
T sl;
BST<T> links,rechts;
};
//template<class T>
//BST<T>& BST<T>::operator=(BSTknoopptr<T>&& other){
// cout<<"called BST<T> move with BSTknoopptr r-value ref argument"<<endl;
// (*this).BSTknoopptr<T>::operator=(move(other));
// return *this;
//}
template<class T>
void BST<T>::add(const T& value){
if(value <= this->get()->sl){
if(this->get()->links != nullptr){
this->get()->links.add(value);
}
else {
//BSTknoopptr<T> tmp(new BSTknoop<T>(value)); if used with my own BST<T>& BST<T>::operator=(BSTknoopptr<T>&& other)
BST<T> tmp(new BSTknoop<T>(value));
this->get()->links = move(tmp);
}
}
else{
if(this->get()->rechts != nullptr){
this->get()->rechts.add(value);
}
else{
//BSTknoopptr<T> tmp(new BSTknoop<T>(value)); if used with my own BST<T>& BST<T>::operator=(BSTknoopptr<T>&& other)
BST<T> tmp(new BSTknoop<T>(value));
this->get()->rechts = move(tmp);
}
}
}
template<class T>
istream& BST<T>::lees(istream& is){
string lijn;
T knoopwaarde;
getline(is,lijn);
knoopwaarde = stoi(lijn);
BST<T> temptree(new BSTknoop<T>());
temptree->sl = knoopwaarde;
for(int i=1; i<DATA_SET_LENGTH ; i++){
getline(is,lijn);
knoopwaarde=stoi(lijn);
temptree.add(knoopwaarde);
cout<<temptree;
}
*this = move(temptree);
return is;
}
main.cpp
int main(){
ifstream ifs;
ifs.open("c.dat");
if(ifs.is_open()){
BST<int> tree;
tree.lees(ifs);
tree.schrijfKnoop(cout);
}
else {
cerr<<"failed to open file"<<endl;
return -1;
}
}
Related
These are how I implemented the swap and copy functions for my queue.
Is there a better way?
template <class T>
void Queue<T>::copy(Queue<T> const & other)
{
if(this == &other)
return;
if(m_size != 0)
this->clear();
this->m_cap = other.m_cap;
this->enqueue(other);
}
template <class T>
void Queue<T>::swap(Queue<T> const & other)
{
if(this == &other)
return;
std::swap(this->m_front, other.m_front);
std::swap(this->m_back, other.m_back);
std::swap(this->m_size, other.m_size);
std::swap(this->m_cap, other.m_cap);
}
Thank you:)
Instead of a copy method, you should instead implement a copy constructor. It would be strange for the copy constructor to be passed an instance of itself. If you insist on checking, you can use an assertion.
template <class T>
Queue<T>::Queue(Queue<T> const & other)
: m_cap(other.m_cap), m_size(0), m_front(), m_back()
{
assert(&other != this);
enqueue(other);
}
Your copy method is actually an assignment. It is more natural to implement an assignment operator. This can be accomplished by following the copy-swap idiom.
template <class T>
Queue<T> & Queue<T>::operator = (Queue<T> other)
{
swap(*this, other);
return *this;
}
There is also an idiomatic way to implement swap (taught to me by Mooing Duck a long time ago):
template <class T>
class Queue {
//...
friend void swap(Queue &a, Queue &b) {
using std::swap;
swap(a.m_front, b.m_front);
swap(a.m_back, b.m_back);
swap(a.m_size, b.m_size);
swap(a.m_cap, b.m_cap);
}
};
This way, you can use argument dependent lookup (ADL) to pick the type specific swap implementation if available. And now Queue itself has such an implementation, which is used by the assignment operator. But it can also be used in the case Queue is put inside an object that wants to implement swap for itself.
I am trying to make a little wrapper class such as
template <typename T>
class EdgeTriggeredState
{
public:
void Tick()
{
oldData = newData;
}
EdgeTriggeredState& operator =(const T& v)
{
newData = v;
return *this;
}
// T& operator = (void)
// {
// return oldData;
// }
// T& operator T()
// {
// return oldData;
// }
private:
T oldData;
T newData;
};
Basically I want to be able to directly assign to a variable of type T the value wrapped by the class. I have tried implementing both an assignment (to type T) operator and a cast operator to type T. I am a bit rusty on my C++ as I have been working solely in C. Is there a way to go about implementing this without creating a named getter method?
When I uncomment the first implementation attempt I get error
"../EdgeTriggeredState.h:19:21: error: ‘T& EdgeTriggeredState::operator=()’ must take exactly one argument"
When I uncomment the second implementation (and comment out the first) I get error:
"../EdgeTriggeredState.h:24:16: error: return type specified for ‘operator T’"
When you write an operator T, the return type is implicit, so your code should look something like:
template <typename T>
class DumbWrapper {
T oldData;
T newData;
public:
DumbWrapper& operator = (const T& val) {
newData = val;
return *this;
}
operator T() {
return oldData;
}
};
[Also note the semicolon at the end, and the fact that the constructor and conversion operator were probably intended to be public.]
I have to overload the insertion operator in order to view my class objects in matrix format. I wrote the code but something's wrong. When I include this to my code and trying to build, compiler gives me tons of errors; when I commented that part out, the errors are gone and program works correctly. Here is the code:
template <class itemType>
ostream & operator<< (ostream & os, const Storage2D<itemType> & rhs)
{
node<itemType>* ptrRow = rhs.head;
node<itemType>* ptrColumn = rhs.head;
for(; ptrColumn->down != NULL; ptrColumn = ptrColumn->down)
{
ptrRow = ptrColumn;
for(; ptrRow->right != NULL; ptrRow = ptrRow->right)
{
os << ptrRow->info << setw(10);
}
os << ptrRow->info << setw(10) << endl;
}
return os;
}
Here is how I tried to use overloading from main function:
Storage2D<double> m(row, column);
cout << m;
It is not the member function of class Storage2D, it is written outside of the scope of class Storage2D in the implementation file.
It would be great if you help me, thanks in advance.
EDIT: Here is the rest of my code. The Storage2D.h file:
template <class itemType>
struct node
{
itemType info;
node* right;
node* down;
node()
{}
node(itemType data, node* r = NULL, node* d = NULL)
{
info = data;
right = r;
down = d;
}
};
template <class itemType>
class Storage2D
{
public:
Storage2D(const int & , const int & ); //constructor
//~Storage2D(); //destructor
//Storage2D(const Storage2D & ); //deep copy constructor
private:
node<itemType>* head;
};
ostream& operator<< (ostream & os, const Storage2D & rhs);
#include "Storage2D.cpp"
head is private so the operator needs to be a friend so it can access that data member. It also needs to be declared as a function template since Storage2D is a class template:
#include <iostream> // for std::ostream
template <class itemType>
class storage2D {
// as before
template <typename T>
friend std::ostream& operator<< (std::ostream & os, const Storage2D<T> & rhs);
};
// declaration
template <typename T>
std::ostream& operator<< (std::ostream & os, const Storage2D<T> & rhs);
Note that I have explicitly used std::ostream, since ostream is in the std namespace.
So I have two classes inside the same file; ArrayLinkedList and ArrayLinkedListRow
Inside the first mentioned one I have a method
template<class T> friend ostream& operator <<(ostream& s, ArrayLinkedList<T>& ll){
//Extra code for giving s content
return s;
}
as well as having
template<class T> friend ostream& operator <<(ostream& s, ArrayLinkedListRow<T>& ll){
//Extra code for giving s content
return s;
}
inside of ArrayLinkedListRow.
I get the following error
Error 1 error C2995: 'std::ostream &operator <<(std::ostream
&,ArrayLinkedList &)' : function template has already been
defined
and it's driving me nuts not knowing how to fix it. I have done my research, but still I cannot figure out what to do. I strongly believe that the two classes might be related in the problem, despite of the error just pointing out to one line.
Extra Info:
This is the class ArrayLinkedList header for those who feel confused with my short explanation.
template<class DT>
class ArrayLinkedList {
private:
DT* _info[MAX_SIZE]; // store data
int _next[MAX_SIZE]; // next node
int _nextEmpty[MAX_SIZE]; //next empty slot
ArrayClass< ArrayLinkedListRow<DT> >* _rows;
int _head; // head of the list
int _firstEmpty; // first empty slot
int _size;
void copy(const ArrayLinkedList<DT>& ll);//copy from another list
// add a new node with next as it's next node and returns the index of new node
int newNode( DT& newObject, int next);
public:
ArrayLinkedList(); // empty and copy constructors
ArrayLinkedList(const ArrayLinkedList<DT>& ll);
//copy constructors linked list object to an existing object. This is a deep copy.
~ArrayLinkedList(); // destructor
ArrayLinkedList(DT& newObject); // Constructor that create a list with newObject as the head
ArrayLinkedList(int capacity); // Constructor with a give capacity
ArrayLinkedList(DT& newObject,int capacity);// Constructor with newObject as the head and capacity
bool isEmpty(); // is the list empty?
int size(); // return the number of nodes stored
void add(DT& newObject); // add an object to the tail
void insertAt(DT& newObject, int position); // insert an object at the position specified
DT remove(); // remove the head
DT removeAt(int position); // remove an object at the position specified
int find(DT key); // find the object that matches key, index of the object
void operator=(const ArrayLinkedList<DT>& ll); // = operator
// overloading [] operator, return a reference to object at the
// Add a new data element to the start of a linked list.
DT& operator[] (const int position); // position in the linked list
// ostream operator
template<class T> friend ostream& operator <<(ostream& s, ArrayLinkedList<T>& ll){
return s;
}
void displayRaw(); // display raw data of the data members
};
Try to remove the template<class T> part:
friend ostream& operator <<(ostream& s, ArrayLinkedList& ll){
//Extra code for giving s content
return s;
}
// and analogically with ArrayLinkedListRow
The reason why this works is stated here:
If you declare a variable ArrayLinkedList<int>, then and only then the operator << is created with the template-parameter T and DT (which is unused). If you compile this, everything works fine.
If you add a variable of type ArrayLinkedList<float>, then the operator gets defined a second time and this creates the error.
Working only with DT makes it work as expected.
This prints an error message about qualifiers but don't really understand what that means and how to adjust the code for it to work? Anyways, thanks a lot for looking at the code.
Note: The ostream operator is friended in the Node class.
using namespace std;
ostream& operator(ostream& output, const Node* currentNode)
{
return output;
}
void Node::nodeFunction()
{
//This node has items attached to the 'this' statement. After
//the necessary functions, this is called to output the items.
cout << this;
}
Your overloaded stream operator declaration should be like this:
std::ostream& operator<<(std::ostream& os, const T& obj);
^^^^^^^^^^^^^
You should be returning a reference to object of std::ostream, the & is wrongly placed in your overloaded function prototype.
Have a look at the working sample here.
Adding the source code here, for completeness.
Note: I have taken class Node members as public for ease of demonstration.
#include<iostream>
using namespace std;
class Node
{
public:
int i;
int j;
void nodeFunction();
friend ostream& operator <<(ostream& output, const Node* currentNode);
};
ostream& operator<<(ostream& output, const Node* currentNode)
{
output<< currentNode->i;
output<< currentNode->j;
return output;
}
void Node::nodeFunction()
{
//This node has items attached to the 'this' statement. After
//the necessary functions, this is called to output the items.
cout << this;
}
int main()
{
Node obj;
obj.i = 10;
obj.j = 20;
obj.nodeFunction();
return 0;
}
The & on the return value of the operator is in the wrong place, and it's generally better to use references rather than pointers for ostream operators:
ostream& operator<<(ostream &output, const Node ¤tNode)
{
// Output values here.
return output;
}
void Node::nodeFunction()
{
cout << *this;
}