473,387 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Operator overloading problem

49
I need to create an overloaded cout that will print the contents of an array.

so I can say output << a << endl;

and it will print the contents of the object a... which happens to be an array.

class info:
Expand|Select|Wrap|Line Numbers
  1.  
  2. class List 
  3. {
  4.     public:
  5.  
  6.         List();
  7.  
  8.         bool empty(); //returns true of false if empty
  9.  
  10.         void front(); //makes current position at beginning of list
  11.  
  12.         void end(); //makes current position at the end of list
  13.  
  14.         void prev(); //places current position at the previous element in the list
  15.  
  16.         void next(); //places current position at the next element in the list
  17.  
  18.         int getPos(); //returns current position or where you are in the list
  19.  
  20.         int SetPos(int); //places current position in a certain position in the list
  21.  
  22.         void insertBefore(int); //inserts a new element before the current position
  23.  
  24.         void insertAfter(int); //inserts a new element after the current position
  25.  
  26.         int getElement(); //returns the one element that current position is pointing to
  27.  
  28.         int size(); //returns the size of the list(number of elements in the list)
  29.  
  30.         void replace(int); //replace the current element with a new one
  31.  
  32.         void erase(); //deletes the current element
  33.  
  34.         void clear(); //makes the list an empty list
  35.  
  36.         void reverse(); //reverse elements in a list
  37.  
  38.         void swap(List);  //swaps all the elements of one list with another list
  39.  
  40.         int getMax(); //return Max Size based on const CAPACITY
  41.  
  42.         void display(); //displays contents of list
  43.  
  44.         private:
  45.  
  46.     int current;
  47.     int length;
  48.     static const int CAPACITY = 20;
  49.     int arrayList[CAPACITY];
  50.     int arrayCopy[CAPACITY];
  51.  
  52. };
  53.  
  54.  

constructor:
Expand|Select|Wrap|Line Numbers
  1. List::List()
  2.         {
  3.             //constructor - initialize array's at position 1
  4.             int maxLen=CAPACITY;
  5.  
  6.             length=0;
  7.             current=0;
  8.             arrayList[length];
  9.             arrayCopy[length];
  10.             //clean up garbage...
  11.             for(int i=0;i<CAPACITY;i++)
  12.                 {
  13.                     arrayList[i]=0;
  14.                     arrayCopy[i]=0;
  15.                 }
  16.         }
  17.  
  18.  
I have one overloaded method... which returns the size of the array

Expand|Select|Wrap|Line Numbers
  1.  
  2. ostream& operator <<(ostream& output,  List& out)
  3.     {
  4.  
  5.         output << out.size()<< endl;
  6.  
  7.         return output;
  8.     }
  9.  
  10.  
I understand this code, I am simply calling the size method from the program, but i don't know how to pass in the array so that i can print it line by line... simple syntax i am sure... but the whole thing is baffling me... I need to be able to call this on any variation of the class, so it cannot be specific to any one array.
Sep 22 '14 #1
4 1279
weaknessforcats
9,208 Expert Mod 8TB
Isn't the array:

Expand|Select|Wrap|Line Numbers
  1. int arrayList[CAPACITY];
?

That is, the array member of your List class?

So the overload you have:

Expand|Select|Wrap|Line Numbers
  1. ostream& operator <<(ostream& output,  List& out)
  2.      {
  3.         output << out.size()<< endl;
  4.  
  5.          return output;
  6.      }
  7.  
  8.  
already has the array because it's inside the List object named out. However, this function is not a member function of List so you have no access to the array. What you want to so is:

Expand|Select|Wrap|Line Numbers
  1. ostream& operator <<(ostream& output,  List& out)
  2.      {
  3.        for (int x =0; x< out.size(); ++x)
  4.        {
  5.         output << out[x];
  6.        }       
  7.  
  8.          return output;
  9.      }
  10.  
  11.  
Unfortunately, out[x] won't compile until you overload the index operator in List:

Expand|Select|Wrap|Line Numbers
  1. int List::operator[](int a)
  2. {
  3.     return arrayList[a];
  4. }
Keep me posted on how you are coming along.
Sep 22 '14 #2
sooli
49
well, you gave me an idea... and it seems to be working - instead of overloading the list operator, I used my existing methods to produce the data I wanted by creating a loop:

[code]

ostream& operator <<(ostream& output, List& out)
{

output << out.size()<< endl;
out.front(); //sets position to the front of array
for (int i=0; i<out.size(); i++) //increment index i thereby stepping thru array
{
output << out.getElement() << " " ; //print out value of position i
out.next(); //advances current position by one in array

}
return output;
}

Thanks for jogging the brain!
Sep 22 '14 #3
weaknessforcats
9,208 Expert Mod 8TB
Yep. Looks like a solution.

In fact, by not modifying the List class you have increased encapsulation since there is now less code dependent upon the list class implementation. On a real job, this will get you noticed.
Sep 22 '14 #4
sooli
49
@ Weaknessforcats: Thanks for the encouragement!
Sep 29 '14 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Matti | last post by:
Hello, I have a problem comparing 2 values of type AnsiString using the == operator (C++ Builder 5). I use another class called TValue, which also defines the == operator. Now when I try to...
0
by: Martin Magnusson | last post by:
I have defined a number of custom stream buffers with corresponding in and out streams for IO operations in my program, such as IO::output, IO::warning and IO::debug. Now, the debug stream should...
6
by: titan0111 | last post by:
----inside of class called snowfall, i wrote---- int operator == (char); ----and right before main() i wrote---- int snowfall::operator == (char str) // comparing two member data...
51
by: Jojo | last post by:
Is there any way to get to the left-hand side of an operator? Consider the following (this is not meant to be perfect code, just an example of the problem): class Matrix { public: int data;...
2
by: A_StClaire_ | last post by:
hopefully this code is not too obscure. lemme know and I can provide clarification. basically trying to implement a linked list (single links) manually. the function below is supposed to assign...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
3
by: Peterwkc | last post by:
Hello all expert C++ programmer, i fairly new to C++ programming. I have a class cellphone which contain dynamic pointer. I have create (example)ten cellphone. I want to ask user for the...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
4
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Operator overloads are just like any other member function, you can make them do whatever you want. However, of course, we might expect them to behave in a certain way. The ++ operator should...
1
by: abnair | last post by:
I am trying out operator overloading. I have following code: class Mystring{ public: Mystring(char* ch) { data = new char; strcpy(data,ch); } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.