C++ operator overloading [] - c++

i have a little problem with my code in c++.. I need to doit this way, because it's work to school..
I have template named Catalog
template <typename T>
class Catalog
{
struct Item{
T* _product;
unsigned int _amount;
Item* _next = nullptr;
};
Item* _head;
Item* _actual;
Item* _last;
int _size;
void init()
{
this->_size = 0;
this->_head = nullptr;
this->_actual = nullptr;
};
public:
Catalog(void)
{
this->init();
};
T*& operator[](unsigned int){
Item* node = this->_head;
for (int i = 0; i < this->_size; i++)
{
if (i == pos)
{
return &node->_product;
}
node = node->_next;
}
return nullptr;
};
};
It's a structure where i have Items and in Items i have pointer to next one in array..
I tryed to like this
Catalog<Products> *catalog = new Catalog<Products>();
Products *pr1 = new ProductA(5, "jmeno", 5);
catalog->Add(pr1, 5);
Products* ct = catalog[0];
In my case visual studio is reporting this error
IntelliSense: no suitable conversion function from SemestralniPrace::Catalog<Products>" to "Products *" exists
I want to correct operator[] so i can use my catalog like i need to.. or correct the code in main..
It's for example, i have some more functions in class Catalog, but it isn't importnent for this problem..
Can someone help me please.. Even suggestions is good for me. I am desperate with this.
Thanks and sorry for my English, it's not mine native language.

You have two mistakes. The first one is relative to the return statement of the operator []. The type of expression &node->_product is T** while the return type of the operator is T*&
You have to write simply
return node->_product;
The second mistake is relative to statement
Products* ct = catalog[0];
You have to write either
Products* ct = ( *catalog )[0];
or
Products* ct = catalog[0][0];

Related

Implement iterator of a circular list

I'm trying to implement a class of Circular List with a nested class of iterator and I wrote like this:
template <class T>
class CircularList {
struct Item {
T data;
Item* next;
};
Item* head;
int size;
public:
CircularList() {
head = new Item();
head->next = head;
}
int sizeList() { return size; }
void push(T data) {
Item* i = new Item();
i->data = data;
i->next = head->next;
head->next = i;
size++;
}
class CircularListIterator {
Item* p;
CircularListIterator() {
p = head->next;
}
bool hasNext() {
if(p->next != head) {
return true;
}
return false;
}
T next() {
T data_temp = p->data;
p = p->next;
return data_temp;
}
};
CircularListIterator* iterator() {
return new CircularListIterator();
}
};
int main() {
CircularList<string>* letters = new CircularList<string>;
letters->push("d");
letters->push("c");
letters->push("b");
letters->push("a");
Iterator<string>* it= new Iterator<string>;
it = letters->iterator();
while (it->hasNext()) {
cout<< it->next() << "," << endl;
}
return 0;
}
But the Iterator is not working when I try to create an iterator in the main function, It said that it wasn't declared in the scope and has no member of it.
Assuming by "in the main class" you mean in the main function, the problem is quite straightforward: you're trying to construct a ::Iterator<string>, but there is no class in the global namespace (or anywhere else, in this code sample) called Iterator! You could try constructing a CircularList<string>::CircularListIterator - that's at least a class that exists - but it wouldn't work because the iterator needs to be associated with a CircularList object for it to be able to access member variables like head.
The correct thing to do here is to promote the iterator function - the one that returns a CircularListIterator* - out of the CircularListIterator class and into the CircularList class. Then, in your main function, you can call letters->iterator() and it'll return a CircularListIterator* for the letters object.
Now, CircularListIterator doesn't inherit from any other iterator classes - neither the (nonexistent-in-this-code Iterator you've typed it as, nor the C++ std::iterator or any of its variants) - so you can't assign it to it or probably even compile the code that references Iterator. To make CircularListIterator a subclass of std::iterator, you'll need to extend std::iterator<Category, T> with the appropriate category. See https://www.cplusplus.com/reference/iterator/iterator/ for more information on the std::iterator class template, including an example of implementing it.

Overloading < operator in template

I'm programming a binary tree class template. When I add a new node I check if the new one is less or greater than current.
I overloaded < and > operator in the class type (Object) and works properly but the template doesn't call the overloaded operator, instead uses the generated by the compiler.
object.cc - Comparison between objects works outside the template
bool Object::operator<(const Object& par_other) const
{
printf("\n <");
return id_ < par_other.id_; //assume that you compare the record based on a
}
EDIT 1: Added some requested code. Thx for the help :)
struct Node {
T* value;
Node* left;
Node* right;
};
template <class T>
void BinaryTree<T>::add(T* par_T, Node* par_node) {
if (par_node == nullptr) {
par_node->left = nullptr;
par_node->value = par_T;
par_node->right = nullptr;
} else {
if (par_node->value == nullptr) {
par_node->value = par_T;
} else if (par_node->value > par_T) {
if (!par_node->right) {
par_node->right = createNode();
}
add(par_T, par_node->right);
} else if (par_node->value < par_T) {
if (!par_node->left) {
par_node->left = createNode();
}
add(par_T, par_node->left);
}
}
Why does a node have a T* instead of a T?
If you have a good reason to do that, then compare with:
*par_T < *(par_node->value)
and
*(par_node->value) < *par_T
Notice the use of * and notice I switched the sides rather than misuse >
If you didn't have a good reason to have a node contain a T* then get rid of the *s in this code (and correspondingly elsewhere) but still remember not to use >, ==, != etc. They all can be inferred by results of < ( a is "equal to" b when both a<b and b<a are false)
You also need to fix more things than you asked about. You seem generally confused about the nature of pointers. An extreme example from your code:
if (par_node == nullptr) {
par_node->left = nullptr;
par_node->value = par_T;
par_node->right = nullptr;
}
Think about what that code is doing!

How to return pointer from linked list (in Class Template)

I've stumbled upon a problem with my linked list class.
I've one abstract class Shape and multiple classes inheriting from it, like Square or Triangle etc.
I'm storing them in my List class but I don't know how to return stored object back to the pointer of Shape.
Since my explanation may seem pretty vague here is some code with expected behaviour explained.
class Shape // abstract class
{
public:
int a;
//some member virtual methods
};
class Square : public Shape
{
//using the virtual methods from Shape
};
In my main file, this is how I want to use it:
int main()
{
List<Shape*> ShapeList;
Shape *ptr;
Square a(2, 1, 1); // size, x, y coordinates
ShapeList.add(ptr);
//up to this point everything works well
// now I want my list to return a pointer to it's member
// so I can modify it
Shape *listptr;
listptr = ShapeList.findInstanceAt(0); // here's my error
listptr->a = 5; // what I want to do next
}
So as you can see I'm havingtroubles with returning proper value from my list and I don't know how to solve this.
Here's my simplified list implementation:
template <class T> class Node
{
T data;
Node *next;
public:
inline T getData()
{
return data;
}
inline Node* getNext()
{
return next;
}
};
template <class T> class List
{
Node<T> *head, *tail;
public:
List() : head(NULL), tail(NULL) { }
T* findInstanceAt(int _k)
{
if (NULL == head)
{
cout << "\nList is empty.";
return NULL;
}
else
{
Node<T> *temp = new Node<T>;
temp = head;
for (size_t k = 0; k < _k; ++k)
{
if (NULL != temp->getNext()) temp = temp->getNext();
else return NULL;
}
return temp->getData;
}
}
}
Thanks in advance for any suggestions on how to make this work.
#EDIT
Ahh I forgot to add compiler errors that I'm getting:
Error 1 error C2440: '=' : cannot convert from 'Shape **' to 'Shape *'
Do you want to store Shapes or pointers to Shapes in the list? And do you want the findInstanceAt to return the node in the list or a pointer to the node in the list? At the moment you are not consistent on these things
You store Shape* nodes in the list but the findInstanceAt returns a pointer to the node - which is a Shape** object. This is what the compiler is complaining about
You probaly need to chang
T* findInstanceAt(int _k)
to
T findInstanceAt(int _k)

Mixing abstract classes and templates, a recipe for disaster?

I'm having problems with the following situation. I have three classes that are involved in this mixup. List, ListNode, City. I have a List<City *>, where the list will be made up of a set of ListNode<City *> (standard wrapper around the list nodes).
City is an abstract class, so there are several classes that inherit from it that could be placed in this list and accessed polymorphically. The List class has a getHead() method which returns a pointer to a ListNode that is the head.
Any city has a population, so to access the populations, I'd expect the following to work. It's not, thus my question. I broke it down into pieces to make it simpler along the way:
ListNode<City *> *head= country->city_list->getHead();
City *headnode = *head->getNode();
cout << "Test: " << headnode->getPopulation() << endl;
getPopulation() returns an integer. country is defined as List<City*> *city; Any help on how I could figure out my problem would be greatly appreciated.
edit adding more code for better idea of what I'm working with. First, ListNode:
template <class T>
class ListNode
{
public:
ListNode() {next = 0;node = 0;};
ListNode(T *t) {node = t; next = 0;};
ListNode(const ListNode &l)
{
//long copy constructor. snip.
};
T *getNode() const { return node; }
ListNode *getNext() const { return next; };
private:
T *node;
ListNode *next;
};
Now, here is what might relevant in the List class..
template <class T>
class List
{
public:
List()
{
head = 0;
size = 0;
};
List(ListNode<T> *t)
{
head = t;
size = 1;
};
List(T *t)
{
head = new ListNode<T>(t);
size = 1;
};
List(const List<T> &t)
{
// long copy constructor. snip.
};
//bunch of irrelevent methods.
ListNode<T> *getHead() const {return head;};
List &operator+=(T &t)
{
this->insert(&t);
size++;
return (*this);
};
private:
List &insert(T *t)
{
ListNode<T> *current = head;
if (current == 0)
{
head = new ListNode<T>(t);
}
else
{
while (current->getNext() != 0)
{
current = current->getNext();
}
current->setNext(new ListNode<T>(t));
}
return (*this);
};
ListNode<T> *head;
int size;
};
I have a hunch that the process of inserting might be the problem. I insert with the List class's += operator, shown in the List implementation above. It calls the private insert method shown above, as well. It looks like this:
City *somecity = new City(x,y,z); //some parameters. integers.
*city_list += somecity; // where city_list is a List.
I think you've got a variable scoping problem.
Your ListNode class contains a pointer to the node value. Your ListNode constructor takes in a pointer to the node value and saves it.
The problem is if that pointer is to a local variable that then goes out of scope. Your ListNode's node pointer is now pointing to an object that doesn't exist. e.g. in this example
addToList(List<int>& myList)
{
int x = 3;
myList += x; // pointer to x is in the list
}
// Out of scope; x no longer exists, but myList has a pointer to it.
// Accessing this node will result in an error.
There are a couple possible remedies:
Have your ListNode contain values rather than pointers. The drawback here is that you will be making copies of the values
Implement ListNode using a reference counted smart pointer which will manager the lifetime of the object.
Well, what you could do is:
ListNode<City *>* head = new ListNode<City*>(country->city_list->getHead());
City* headnode = head->getNode();
cout << "Test: " << headnode->getPopulation() << endl;
It will take the existing City (on the memory) and put it at the head of the List node, and so on.
and if you want to copy them, maybe you could just make this:
ListNode<City *>* head = new ListNode<City*>*(new City(country->city_list->getHead()));
City* headnode = new City(head->getNode());
cout << "Test: " << headnode->getPopulation() << endl;
Hope it will help you.

use operators in templates in c++

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)