class point2d

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100`` ``````point2d::point2d() { x = y = 0; } point2d::point2d(int initx, int inity) { setx(initx); sety(inity); } int point2d::getx() { return x; } int point2d::gety() { return y; } void point2d::setx(int initx) { if (initx >= 0) x = initx; else x = 0; } void point2d::sety(int inity) { if (inity >= 0) y = inity; else y = 0; } bool point2d::operator==(const point2d& p2){ if (x==p2.x && y==p2.y) return true; else return false; } bool point2d::operator !=(const point2d& point2){ if (x!=point2.x || y!=point2.y) return true; else return false; } point2d point2d::operator +(const point2d& p2){ point2d temp; temp.x=x+p2.x; temp.y=y+p2.y; return temp; } point2d point2d::operator -(const point2d& p2){ point2d temp; temp.x=x-p2.x; temp.y=y-p2.y; return temp; } ostream& operator<<(ostream& a, point2d p2){ a<<"("<>(istream& a, point2d p2){ a>>p2.x>>p2.y; return a; } const point2d& point2d::operator =(const point2d& p2){ if (this != &p2) { this->x=p2.x; this->y=p2.y; } return *this; }``````

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268`` ``````template class LinkedList { public: LinkedList(); LinkedList(LinkedList& list); virtual ~LinkedList(); void addFirst(T element); void addLast(T element); T getFirst() const; T getLast() const; T removeFirst() throw (runtime_error); T removeLast(); void add(T element); void add(int index, T element); void clear(); bool contains(T element) const; T get(int index) const; int indexOf(T element) const; bool isEmpty() const; int lastIndexOf(T element) const; void remove(T element); int getSize() const; T removeAt(int index); T set(int index, T element); Iterator begin() const { return Iterator(head); }; Iterator end() const { return Iterator(tail->next); }; private: Node* head; Node* tail; int size; }; template LinkedList::LinkedList() { head = tail = NULL; size = 0; } template LinkedList::LinkedList(LinkedList& list) { head = tail = NULL; size = 0; Node* current = list.head; while (current != NULL) { this->add(current->element); current = current->next; } } template LinkedList::~LinkedList() { clear(); } template void LinkedList::addFirst(T element) { Node* newNode = new Node(element); newNode->next = head; head = newNode; size++; if (tail == NULL) tail = head; } template void LinkedList::addLast(T element) { if (tail == NULL) { head = tail = new Node(element); } else { tail->next = new Node(element); tail = tail->next; } size++; } template T LinkedList::getFirst() const { if (size == 0) throw runtime_error("Index out of range"); else return head->element; } template T LinkedList::getLast() const { if (size == 0) throw runtime_error("Index out of range"); else return tail->element; } template T LinkedList::removeFirst() throw (runtime_error) { if (size == 0) throw runtime_error("No elements in the list"); else { Node* temp = head; head = head->next; if (head == NULL) tail = NULL; size--; T element = temp->element; delete temp; return element; } } template T LinkedList::removeLast() { if (size == 0) throw runtime_error("No elements in the list"); else if (size == 1) { Node* temp = head; head = tail = NULL; size = 0; T element = temp->element; delete temp; return element; } else { Node* current = head; for (int i = 0; i < size - 2; i++) current = current->next; Node* temp = tail; tail = current; tail->next = NULL; size--; T element = temp->element; delete temp; return element; } } template void LinkedList::add(T element) { addLast(element); } template void LinkedList::add(int index, T element) { if (index == 0) addFirst(element); else if (index >= size) addLast(element); else { Node* current = head; for (int i = 1; i < index; i++) current = current->next; Node* temp = current->next; current->next = new Node(element); (current->next)->next = temp; size++; } } template void LinkedList::clear() { while (head != NULL) { Node* temp = head; head = head->next; delete temp; } tail = NULL; size = 0; } template T LinkedList::get(int index) const { if (index < 0 || index > size - 1) throw runtime_error("Index out of range"); Node* current = head; for (int i = 0; i < index; i++) current = current->next; return current->element; } template int LinkedList::indexOf(T element) const { // Implement it in this exercise Node* current = head; for (int i = 0; i < size; i++) { if (current->element == element) return i; current = current->next; } return -1; } template bool LinkedList::isEmpty() const { return head == NULL; } template int LinkedList::getSize() const { return size; } template T LinkedList::removeAt(int index) { if (index < 0 || index >= size) throw runtime_error("Index out of range"); else if (index == 0) return removeFirst(); else if (index == size - 1) return removeLast(); else { Node* previous = head; for (int i = 1; i < index; i++) { previous = previous->next; } Node* current = previous->next; previous->next = current->next; size--; T element = current->element; delete current; return element; } }``````

My problematic code

 ``12`` ``````list.get(0)=list.get(1); list.get(0).setx(3); list.get(0).sety(3);``````

class linked list is to take point2d as its type but the methods setx&sety as well as my operator= overload function wont change the values of the objects??

Im not sure where the problem is
The prototype of your get() method is `T get(int index) const;`.
In other words, it returns a copy of the stored element.

If you want to modify the content of a stored element, you need to create a non const method that retruns a reference.
I've emailed my teacher for reassurance but I believe that he doesn't want us to alter linkedlist, but rather change our main and point2d class functions to work with it (point2d was created for a previous project)
 but rather change our main and point2d class functions to work with it

The linked list does not give you access to the stored elements, so there's no way you can modify them.

I see two possibilities:
- If you store point2d objects, you have to make a copy, modify the copy, erase the original from the list and insert the copy at the position the original was.
- Store pointers to your point2d objects instead of the objects. It's ugly but it will work. To ease memory management you could use std::unique_ptr or std::shared_ptr.

I'm not that great with pointers and that's a lot of the problem I'm having with this project but uhm...

 ``12`` ``````point2d *point1=new point2d(15,30); cout<<*point1;``````

this code results in an object of point2d with x=15, and y=15?
sorry to be such a noob but im just lost..

implementation of constructor and set methods below

 ``123456`` ``````point2d::point2d(int initx, int inity) { setx(initx); sety(inity); } ``````

 ``12345678910111213141516`` ``````void point2d::setx(int initx) { if (initx >= 0) this->x = initx; else this->x = 0; } void point2d::sety(int inity) { if (inity >= 0) this->y = inity; else this->y = 0; }``````
In your point2d::operator<<() you made a mistake and output twice the X coordinate.
lol... i feel like an idiot
my teacher says that it's not necessary but it's okay to alter LinkedList..
so i tried doing this (just adding a reference operator in front of returned data)

 ``123456789101112`` ``````template T LinkedList::get(int index) { if (index < 0 || index > size - 1) throw runtime_error("Index out of range"); Node* current = head; for (int i = 0; i < index; i++) current = current->next; return &(current->element); }``````

these are the errors that i get:

In file included from main.cpp:20:0:
main.cpp:42:19: instantiated from here
LinkedList.h:280:28: error: conversion from ‘point2d*’ to non-scalar type ‘point2d’ requested

once again i apologize for being so noobish at this.. according to him we got screwed on the academic calendar and we havent went over much of this material
is it possible to make it a friend function to give it access to the private members?
You need not change the body of the function, only its prototype:
 ``123456789101112`` ``````template T& LinkedList::get(int index) { if (index < 0 || index > size - 1) throw runtime_error("Index out of range"); Node* current = head; for (int i = 0; i < index; i++) current = current->next; return current->element; }``````
B-e-a-utiful :D, worked perfect thanks!
all these pointers and reference variable are making my brain hurt >.>
im going to keep this forum up atm because im not done with the project, but thanks again
Topic archived. No new replies allowed.