1

I am writing a Little Man computer simulation and i want to overload the Indexing operator []. I have create a class called LMC and have done the following:

#include <iostream>

using namespace std;

class LMC
{
   public:
       LMC();
       void display();
       int& operator[](int index);
       ~LMC();
   private:
       int **array;
};

LMC::LMC()
{
   array = new int*[100];
   for(int i = 0; i < 100; i++)
   {
       array[i] = new int[3];
   }
   return array;
}

void LMC::display()
{
    for(int i = 0; i < 100;i++)
    {
       for(int j = 0; j <3;j++)
       {
          array[i][j] = 0;
          array[i][2] = i;
          cout << array[i][j]<<" ";
       }
       cout << endl;
    }
 }
 int& LMC::operator[](int index)
 {
    return array[index][2];
 }

  LMC::~LMC()
  {
     for(int i =0; i < 100 ; i++)
     { 
        delete [] array[i];
     }
     delete [] array;
     array = NULL;
  }

  int main()
  {
     LMC littleman;
     while(true)
     {
         int mailbox;
         int function;
         cout << "What is Mailbox number?" << endl;
         cin >> Mailbox;
         cout << "What is the function you want to use?" <<endl;
         cin >> finction;
         //the function is numbers eg 444 and 698;
         littleman.display();
         littleman[Mailbox] = function;
      }
     return 0;
}

I can run the program with no error. When i state that mailbox = 0 and function = 123 the is no problem.

This is displayed:

0 0 0
1 0 0
2 0 0
3 0 0
//continuing to 99

This display is wrong. The following must be displayed:

0 0 123
1 0 0
2 0 0
//continuing to 99

Do i have a logical error or am i overriding the array to display the original and how can i fix it?

tc.
  • 33,468
  • 5
  • 78
  • 96
Johan Dela
  • 85
  • 7
  • 7
    Please save yourself the trouble and use a `std::vector>` instead of `int**` for your internal array. – Tony The Lion Sep 06 '12 at 10:15
  • `0 0 0` Is that the output of `LMC::display()`? – Andriy Sep 06 '12 at 10:16
  • @Andrey Yes that is the output. – Johan Dela Sep 06 '12 at 10:17
  • 2
    @TonyTheLion. You must have more experience than me but i did not ask you for a easy way i asked for help with the code given. If i wanted to use a vector i would have but i did not, no disrespect. – Johan Dela Sep 06 '12 at 10:20
  • How did you manage to run this code? It is not compilable. – Andriy Sep 06 '12 at 10:21
  • 1
    @Johan: The problem is that Tony is _right_. You need to use `std::vector`, and keep such stuff as you are doing until you know more about C++. – sbi Sep 06 '12 at 10:30
  • 1
    @JohanDela What Tony suggested is an improvement to your code. Certain parts of it are unacceptable and have to be rewritten. If you're not using standard library, you should know all well that the responsibility is now on you and the approach is much more error-prone. – Bartek Banachewicz Sep 06 '12 at 10:30
  • 2
    If you have requirements that force you to use `int**` then you'll have to either: reimplement (at least) parts of what is already implemented in `vector` or explain what those requirements are. How can we know whatever solution we propose meets these undisclosed requirements? (in other words, please don't make us waste our time trying to help you) – R. Martinho Fernandes Sep 06 '12 at 10:32
  • 1
    Also, you're violating the "rule of three". Something you don't need to care about if you had used std::vector in the first place. – sellibitze Sep 06 '12 at 10:39
  • How will it work with a vector. Im not as comfortable with vectors as i am with arrays. – Johan Dela Sep 07 '12 at 13:09

2 Answers2

1

Your code has a number of errors which will not let it compile namely:

  • in the LMC() constructor, you have return array;. Constructors never return anything (they don't even have the return type), so you can not use return in them.
  • after void LMC::display(), you have a ;, which is an error, because this is not a definition, but implementation. You should omit it and just leave void LMC::display() { <...> }
  • void LMC::display() is missing the closing } in the end, right before the operator[].
  • in main() you have typos in Mailbox (capital M in one case, and normal m in another. In C+++ Mailbox and mailbox are different variables) and finction instead of function.

As for your problem, you are rewriting the values of the aray in display() function:

array[i][j] = 0;
array[i][2] = i;

That's why you don't see any results.

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
0

These lines

  array[i][j] = 0;
  array[i][2] = i;

in LMC::display() destroy the contents of the array you're trying to display.

Furthermore, there's an extra semicolon at the end of void LMC::display();, so your code is not supposed to compile.

Andriy
  • 8,486
  • 3
  • 27
  • 51