This is to be used in my linked list class. I have a couple of operators already. Im pretty sure I should get around this by changing the return type to
List& and just use my other operator and return the list by reference. i think. and then my equal operator would take care of the rest.
List operator + (int& element,const List& aList)
example this should do L3 = 10 + L1;
add ten to L1 and return a List to L3. I get the error operator + has to many parameters. Iv done this before and havnt had issues. Does anyone know how to fix this. Heres the code for that operator if you are interested.
1 2 3 4 5 6
List operator + (int& element,const List& aList)
{
List temp(aList);
temp.Add_To_Front(element);
return temp;
}
This is an example of poor function design. It's doing too much, it shouldn't really be doing things on two lists if it's an append function.
You're distorting the meaning of +. Conceptually, you can + two lists, but you can't + a list with an element. You can append or something synonymous.
Additionally, if the function changes L1, then L1 can't be passed by const reference. That means don't change L1.
Passing the argument in by reference implies there's a variable to reference. Passing in 10 causes the compiler to make a temporary object that can be referenced.
If you aren't using it to add two objects together don't overload it. Choose another operator or create your own function, as kbw suggests, because the point of operator overload is to redefine an operator to serve the same purpose on some class object as it does on existing primitive types.
Secondly... is your operator a member of the List class? If so it already has one inherent parameter, this. (The class object that it is called upon.) So you only need one operand.
If we are adding one element to a list, we are, generally speaking, appending that element to the list. The result is a new list with all elements of the former list, plus that one.
If we are adding one list to another, then the result is a new list with all the elements of both lists.
The concept of adding lists and elements to lists can be useful, as the original list remains unmodified.
Thats exactly what I was thinking guestgulkan. The reason I thought this is because we had made a clocktime class. And we made a copy constructor and = operator. Then we made + operators for clocktimes and other clocktimes. THen we also Had + operators for clocktimes plus an integer that stood to represent minutes. So I personally would think it to be ok for a user to do
List1+= 10;
List1 = List2+ 10;
List1 = 10 + list2; // append to front
to me it conceptually makes since to be able to use the operator + to add a list together or an element to it.
Also I have been thinking of making an istream operator for for the linked list so you can do the following if you wish.
cin >> list1;
my only issue with this is that I cant seem to figure out how to stop the entrance of numbers to put onto the list.
heres a look at my header file with a bunch of methods I thought would be useful for this class. Let me know what you guys think.
class List
{
public:
List();
List(const List& aList); //copy
List(int* int_array, int size); //constructor takes in an array and puts into list
~List();
int get_count()const;//returns m_count which is modified when list is made
int count()const; //goes down list and counts
bool Delete_From_Front(int& element);
bool Delete_From_Back(int& element);
void Add_To_Front(constint& e);
void Add_To_Back(constint& e);
void Clear_List();// emptys list. Returns it back to default values
void List_To_Array(int*& pTr, int& size); //copy elements of a list to a dynamic array ***user has to delete
void Array_To_List(constint* int_array, int size);// takes an array and stores in list
void print()const;
int Max()constthrow(char*); //returns max#
int Min()constthrow(char*);//returns min#
int Total()const; //returns sum of numbers
float Avg()constthrow(char*); // fix throw************************
int get_last_element()const;
List& operator = (const List& aList); // EX: List1 = List2;
//List operator + (const List& aList)const; // adds two lists together EX: List3 = List1 + List2;
List& operator += (constint& element); // adds a number to end of list EX: List1 += 10;
List& operator += (const List& aList); // EX: L3+=L2
List operator + (int& element,const List& aList);// adds element to beginning of list L3 = 10 + L1;
//List operator + (const List& aList, const int& element)const;// adds element to end of list L3 = L2 + 10;
friend istream& operator >> (istream& is, List& aList); // ?????? how to end
friend ostream& operator << (ostream& os, const List& aList);// print list
//sort
//insert sort??
//reorganize header file etc
private:
ListNode* m_first;
int m_count;
};