I have realized Double Linked List. And now i need to overload == operator.
dll.cpp:67:17: error: expected expression
if ([ind] != sp[ind]) {
The problem i don't understand how to overload == operator if only one parameter is given. I mean if i write bool operator ==(DLL sp1, DLL sp2){} compiler says error: overloaded 'operator==' must be a binary operator (has 3 parameters)
#include<iostream>
#include<string>
using namespace std;
template<typename T>
class DLL {
public:
DLL(){
size = 0;
head = nullptr;
tail = nullptr;
}
T operator [](int ind) {
int counter = 0;
T res = T();
Node *cur = this->head;
while(cur != nullptr) {
if(counter == ind) {
res = cur->data;
}
cur = cur->next;
counter++;
}
return res;
}
bool operator ==(DLL<int> sp){
bool isequal = true;
for(int ind = 0; ind < sp.length(); ind++){
if ([ind] != sp[ind]) {
isequal = false;
}
}
return isequal;
}
void clear() {
while(size != 1)
pop_front();
delete tail;
size--;
}
int length() {return size;}
}
private:
class Node{
public:
T data;
Node *next;
Node *prev;
Node(T data = T(), Node *prev= nullptr, Node *next = nullptr) {
this->data = data;
this->next = next;
this->prev = prev;
}
};
int size;
Node *head;
Node *tail;
};
The way you have it defined as a member function (and it only takes list of int for some reason (you should probably remove the <int>).
bool operator ==(DLL<int> sp); // There is a small issue in that you
// passing by value and thus causing a copy.
// Another issue with this is that it should
// probably marked "const" to indicate state
// is not changed by the call.
When the compiler see's this.
list1 == list2
This is simply syntactic sugar for:
list1.operator==(list2);
Thus is why you only need one parameter when you declare it as a member function. The alternative is to declare it as a friend function.
friend bool operator ==(DLL<T> const& lhs, DLL<T> const& rhs);
In this case it is a free standing function. When the compiler sees:
list1 == list2
This is syntactic sugar for:
operator==(list1, list2)
The problem was that you were defining a member function with two parameters. The left hand side is the class object and then you were expecting two objects on the right hand side (but the == operator only has one place on the right). That is why it is complaining about three parameters.
So the real question is should it be a member or a friend.
It does not matter here.
There are situations where it "can".
Example: If your class contains a single argument constructor (lets say you could create a list from an integer) and you use a member operator==()
DLL<int> list;
if (list == 5) {
}
This will now compile. Because your member operator uses a parameter and the compiler can convert an integer into the DLL parameter using a single argument constructor.
The counter argument to this is that normally you don't want automatic conversions of your type so you should mark single argument constructors as explicit to prevent this.
So:
If you class can automatically be created via a one argument constructor (most cases this is not true but it can be).
Then you should prefer a friend function version.
otherwise it does not matter and I would probably fall towards the member function.
You're doing the right thing for the most part.
The problem that is causing this error:
dll.cpp:67:17: error: expected expression if ([ind] != sp[ind]) {
is that you actually want to do this instead:
*this[ind] != sp[ind]
Also, there appears to be an extra } right here:
int length() {return size;}
} // <- not exactly sure what that's about, but I don't think you need it.
A comparison operator is a binary operator that treats both the operands equally, it is advisable to make it a friend function and not a member.
So the declaration for the function would change to
friend bool operator ==(const DLL<int> & lhs, const DLL<int> & rhs);
You can choose to define it inside or outside the class.
Read here on when you need to make an operator member vs non-member.
Related
I'm trying to write an overloaded assignment operator for my linked list class using a template but I keep getting errors.
Any help with what I'm doing wrong would be great.
The error I'm getting is "out of line definition does not match any declaration in LL".
The declaration I have is:
const LL<T>& operator=(const LL<T> &rhsObj);
and the implementation is:
template<typename T>
LL<T>& LL<T>::operator=(const LL<T>& rhsObj) const
{
if (this!= &rhsObj)
{
//no self assignment so OK to continue
//deallocate left hand side memory
this->clear();
count = 0;
head = NULL;
cout <<"calling function copyList()" << endl;
count = 0;
head = NULL;
string diCode = "";
int onNode = 0;
if(rhsObj.head == NULL)
{
cout <<"other list is empty, nothing to do" << endl;
}
else
{
onNode =0;
Node<T> *otherCurrent = rhsObj.head;
while( otherCurrent != NULL)
{
int duplicateInfo;
duplicateInfo = otherCurrent->info;
push_back(duplicateInfo);
otherCurrent = otherCurrent ->next;
onNode++;
} //END while(otherCurrent != NULL)
} // END else block of if (otherLL.head == NULL)
} // END if (this != &rhsObj)
return *this;
}
In the updated code, the error is because your declaration is:
const LL<T>& operator=(const LL<T> &rhsObj);
but the attempted implementation is:
LL<T>& LL<T>::operator=(const LL<T>& rhsObj) const
The location of const is actually significant. The first one means that the function returns a reference which is const; the second one means you may not modify *this within the function.
A trailing const is part of the function signature, so the compiler does not consider your implementation to correspond to the function that was declared initially. So it looks like you're trying to add a new function that wasn't declared in the class definition, which isn't permitted.
To fix this, make the implementation match the declaration (move the const from the end to the start).
I'm trying to teach myself about classes in C++, and I'm running into a bit of a stumbling block, which I can't seem to clear up. I was hoping someone might be able to point me in the correct direction.
I decided to construct a small Tree class, which constructs a new BST. I want to be able to call certain methods on my object like so:
int main() {
Tree<int> tree1;
tree1.insert(5);
int treeMin = tree1.minValue();
int treeMax = tree1.maxValue();
tree1.printTree();
}
Right now, in order to call these functions, I am defining both public and private functions so that you don't call function in a redundant manner. for instance:
(what I'm trying to avoid)
int main() {
Tree<int> tree1;
tree1.insert(tree1, 5);
int treeMin = tree1.minValue(tree1);
int treeMax = tree1.maxValue(tree1);
tree1.printTree(tree1);
}
In order to do avoid having this redundancy, I am defining a public and private version of the same function. In this way, the public functions call their private counterparts.
template<class T>
class Tree {
private:
treeNode<T>* root;
treeNode<T>* newNode(T data);
void insert(treeNode<T>*& root, T data);
int minValue(treeNode<T>*& root);
int maxValue(treeNode<T>*& root);
void printTree(treeNode<T>*& root);
public:
Tree();
~Tree();
void insert(T data);
int minValue();
int maxValue();
void printTree();
};
And then, as an example:
template<class T>
int Tree<T>::minValue() { minValue(root); }
template<class T>
int Tree<T>::minValue(treeNode<T>*& root) {
if (root == NULL) { return 0; }
if (root->left == NULL) { return root->data; }
else { minValue(root->left); }
}
So, my question is:
If I'm writing my functions recursively, I understand that I need to declare a private function that accepts an argument, but is this considered a bad style? Is this sloppy?
Thanks for your help!
The private member functions in your code are only a needless complication. I would just move their code to the public member functions: less code, more clean code, less indirection so more directly grokable code, all nice. For some of them you might support reuse by making them free functions in a details namespace, but I think that would be premature generalization, expending effort on possible reuse that probably won't take place.
Example code at end of answer.
Re another design issue, declaring
int minValue();
int maxValue();
precludes calling these member functions on a const object. Instead do
int minValue() const;
int maxValue() const;
A third issue, it's generally a Really Bad Idea™ to do i/o in a non-i/o class. If you print the tree to standard output, how would you use the class in a GUI program? So, instead of
void printTree();
do e.g.
ostream& operator<<( ostream& stream ) const;
or e.g.
string toString() const;
A fourth issue, you need to take charge of copying – read up on the “rule of three” and the “rule of zero”.
The simplest way to do that is to replace
treeNode<T>* root;
with
unique_ptr< treeNode< T > > root;
where unique_ptr is std::unique_ptr.
Alternatively declare at least a copy constructor and a copy assignment operator, or inherit from a “non-copyable” class. To make the class effectively non-copyable, you can make these operators private or protected. To make it copyable, make them public and do the right thing in each (a good default implementation of the copy assignment operator is to express it in terms of copy construction via the copy-and-swap idiom, which means introducing a non-throwing swap function).
A fifth issue is that the implementation
template<class T>
int Tree<T>::minValue(treeNode<T>*& root) {
if (root == NULL) { return 0; }
if (root->left == NULL) { return root->data; }
else { minValue(root->left); }
}
strongly suggests that each node stores a value that's implicitly convertible to int. You don't provide the declaration of treeNode. But this looks like a design level bug, that the intent was for minValue to return a T, not an int – and ditto for maxValue.
A very small coding issue (not design level): in C++11 and later you should preferentially use nullptr, not NULL.
nullptr can be freely passed through argument forwarding functions, while NULL then suffers a decay to integral type, since NULL is just a zero-constant of integral type.
nullptr does not require that you include any header, while NULL is defined by a header, i.e. with nullptr you avoid a header dependency.
Finally, regarding
if (root == NULL) { return 0; }
for the minValue, this may of course be the intention, the design. But possibly you want to either signal failure or treat the call as a logic error.
To treat the call as an error, assert( root != nullptr ); and provide a means for the client code to check for empty tree.
To signal failure, either return an object with optional value (e.g. like boost::optional or Barton/Nackmann's original Fallible), or throw an exception (the std::runtime_error class is a good general default exception class choice).
It's also possible to combine the two approaches, to provide both, perhaps with names like minValue and minValueOrX.
More generally it's sometimes possible to reserve some special value as a "no such" indicator. E.g. std::numeric_limits<T>::min(). But this makes for brittle code, since such a value can easily occur naturally in the data, and since client code may easily fail to check for the special value.
Example, coded for C++11:
#include <assert.h>
#include <iostream> // std::cout, std::endl
#include <string> // std::string
namespace my {
using std::string;
template<class T>
class Tree
{
private:
struct Node
{
T value;
Node* p_left;
Node* p_right;
auto to_string() const -> string
{
using std::to_string;
string const left = (p_left == nullptr? "" : p_left->to_string());
string const right = (p_right == nullptr? "" : p_right->to_string());
return "(" + left + " " + to_string( value ) + " " + right + ")";
}
~Node() { delete p_left; delete p_right; }
};
Node* root_;
Tree( Tree const& ) = delete;
Tree& operator=( Tree const& ) = delete;
public:
auto is_empty() const -> bool { return (root_ == nullptr); }
void insert( T const data )
{
Node** pp = &root_;
while( *pp != nullptr )
{
auto const p = *pp;
pp = (data < p->value? &p->p_left : &p->p_right);
}
*pp = new Node{ data, nullptr, nullptr };
}
auto minValue() const -> T
{
assert( root_ != nullptr );
Node* p = root_;
while( p->p_left != nullptr ) { p = p->p_left; }
return p->value;
}
auto maxValue() const -> T
{
assert( root_ != nullptr );
Node* p = root_;
while( p->p_right != nullptr ) { p = p->p_right; }
return p->value;
}
auto to_string() const -> string
{
return (root_ == nullptr? "" : root_->to_string());
}
~Tree() { delete root_; }
Tree(): root_( nullptr ) {}
Tree( Tree&& other ): root_( other.root_ ) { other.root_ = nullptr; }
};
} // namespace my
auto main() -> int
{
my::Tree<int> tree;
for( int const x : {5, 3, 4, 2, 7, 6, 1, 8} )
{
tree.insert( x );
}
using std::cout; using std::endl;
cout << tree.to_string() << endl;
cout << "min = " << tree.minValue() << ", max = " << tree.maxValue() << endl;
}
Output:
(((( 1 ) 2 ) 3 ( 4 )) 5 (( 6 ) 7 ( 8 )))
min = 1, max = 8
Solved by added a default constructor to the symbol struct, but also I would like to ask why there is a call to a default constructor if possible. If not, it's fine. thanks.
I'm trying to write a tree, but when I define a node like so:
TreeNode<SymbolPriority>* treeRoot =
new TreeNode<SymbolPriority>(SymbolPriority('a', 1));
I can't compile and it throws an Error c2512 'SymbolPriority': no default appropriate default constructor; however, in my struct I have the constructor I am trying to use, and I have used it before, so I do not know what is going on.
I have tried this:
SymbolPriority aSymbol( 'a', 1 );
TreeNode<SymbolPriority>* treeRoot = new TreeNode<SymbolPriority> (aSymbol);
but it doesn't work either.
I put down the relevant code below:
template<typename DATA_TYPE> struct TreeNode
{
TreeNode(const DATA_TYPE& value, TreeNode* left = NULL, TreeNode* right = NULL)
{
Value = value;
Left = left;
Right = right;
}
DATA_TYPE Value;
TreeNode* Left;
TreeNode* Right;
bool IsLeaf() const
{
return Left == NULL && Right == NULL;
}
};
and
struct SymbolPriority
{
SymbolPriority(char aSymbol, int priority){
Symbol = aSymbol;
Priority = priority;
};
char Symbol;
int Priority;
bool operator > (const SymbolPriority& compareTo) const{
return (Priority > compareTo.Priority );
};
bool operator < (const SymbolPriority& compareTo) const{
return !( *this > compareTo);
};
bool operator==(const SymbolPriority& compareTo) const{
return (Priority == compareTo.Priority );
};
};
Default constructors are used to initialize objects without use of arguments.
To fix this, simply put:
TreeNode() { // NO PARAMETERS
... // default initializing variables, or whatever you'd like
}
In your structures, classes, and any others if they are declared without arguments.
In case you'd like to hear more on the concept of "Default Constructors".
There are quite a few stackoverflow questions relating to Error: C2512.
Consider looking at this:
error C2512: no appropriate default constructor available
Wikipedia happens to give a great explanation on the necessity of default constructors here.
Some notable purposes are:
"When an array of objects is declared, e.g. MyClass x[10];; or allocated dynamically, e.g. new MyClass [10]; the default constructor is used to initialize all the elements"
"When an object value is declared with no argument list, e.g. MyClass x;; or allocated dynamically with no argument list, e.g. new MyClass or new MyClass(); the default constructor is used to initialize the object"
"When a derived class constructor does not explicitly call the base class constructor in its initializer list, the default constructor for the base class is called"
That last one is important for inheritance.
I have a code of doubly Linked list and there are few lines of that code which idea is not clear . I request for experts comments on the lines which have comment in the following .Its long since I last time used C++ . There are just two line which i indicated are not understandable for me.
template <typename T>
class double_linked
{
struct node
{
T data;
node* prev;
node* next;
node(T t, node* p, node* n) : data(t), prev(p), next(n) {}
};
node* head;
node* tail;
public:
double_linked() : head( NULL ), tail ( NULL ) {}
template<int N>
double_linked( T (&arr) [N]) : head( NULL ), tail ( NULL )
{
for( int i(0); i != N; ++i)
push_back(arr[i]);
}
bool empty() const { return ( !head || !tail ); } // this doing?
operator bool() const { return !empty(); } // this doing? i know operators need in C++ but dont know the use of it here
void push_back(T);
void push_front(T);
T pop_back();
T pop_front();
~double_linked()
{
while(head)
{
node* temp(head);
head=head->next;
delete temp;
}
}
};
operator bool() const is a conversion operator. If an instance of double_linked is used in a context where a bool is required, this function will be called to do the conversion. (And will evaluate to true if the list is not empty in your case.)
The other function is a plain old function, which will return true if either head or tail is null.
For more about conversion operators, see: How do conversion operators work in C++?
The first is a function to determine if the list is empty. In a doubly linked list, if you have at least one element then the head and tail pointers (which point to the start and the end of the list respectively) must point to a valid element. Therefore, you can test whether the list is empty by testing if both those pointers do not point to a valid element (i.e. are null). That is what the expression !head || !tail does - checks if either pointer is null, and if so the list is empty.
The operator bool() thing is a conversion operator. It basically means, whenever the list is cast to bool, that function is called and the result used as the value of the bool. The function returns whether the list is not empty, so an expression like bool result = myList; will make result be true if the list is not empty.
This is a predicate telling us if it is empty or not, it is checking if the pointers are 0 (null).
bool empty() const { return ( !head || !tail ); } // this doing?
This allows the user to treat a instantiation of the container as a boolean, which is true if it is not empty.
operator bool() const { return !empty(); } // this
Implemented a linked list is a good programming excercise but if you want to use a linked list in your actual code then you should really use std::list (in ).
I am trying to implement a List class using pointers and am trying to implement a function LOCATE(T x) where T is for the template and returns the first position of the element x if found, else returns last position + 1.
My functions code is
template<class T>
int List<T>::locate(T n) const
{
int size = end();
Node<T> * p = head_;
for (int i = 0; i < size; i++)
{
if (p->data() == n) // fails on this line
return i;
p = p->link();
}
return size; // if no match found
}
I initialise my list with T as string as
List<string> myList;
but I get an error message
'bool std::operator ==(const std::istreambuf_iterator<_Elem,_Traits> &,const std::istreambuf_iterator<_Elem,_Traits> &)' : could not deduce template argument for 'const std::istreambuf_iterator<_Elem,_Traits> &' from 'std::string
Why is the error coming up even though the '==' operator is defined for the string class?
'
The code for Node is
template<typename T>
class Node
{
public:
// Constructors
Node();
Node(T d, Node<T> * l = NULL);
//Inspectors
T data() const;
Node<T> * link() const;
// Mutators
void data(T d); // assigns new value to Node
void link(Node<T> * l); // points this Node to a different one
// Destructor
~Node();
private:
Node<T> * link_;
T data_;
};
template<typename T>
T Node<T>::data() const
{
return data_;
}
template<typename T>
Node<T>* Node<T>::link() const
{
return link_;
}
The calling code is
List<string> test;
test.add("abc");
cout << test.locate("abc") << endl;
Without getting neck-deep in your code, I notice several problems.
Firstly,
locate(T n)
should be
locate(const T& n)
This saves a possible copy of n
And to ask a stupid question, are you sure you've done:
#include <string>
Try :
if( n.compare(p->data()) == 0 )
string::compare documentation
As the comments below have noted, operator== should work. Please double check that you have
#include <string>
using std::string;
The reference to std::istreambuf_iterator is peculiar as nothing in the code you show justifies it -- can you please show us Node and whatever other code impinges on this in a minimal failing example? Trying to evince the problem from very partial code and an error message is very much like pulling teeth...!-)
This looks OK, I can't see how the std::istreambuf_iterator gets into the picture...
One thing you may want to adjust is taking const T& instead of T as in-parameters to your methods, e.g.
Node(const T& d, Node<T> * l = NULL);
void data(const T& d);
int List<T>::locate(const T& n) const { ...
What with the actual problem, there's bound to be something else going on.
Start deleting code untill it works again. Some typo or rogue macros or conflicting namespaces screw things up.
Will this compile by itself?
string data = "bla";
Node<string> p("bla");
bool b = p.data() == data;
(Every C++ programmer should make a cout << "bla" << end; typo. Very entertaining)