Thread: Question

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    24

    Question

    Hi

    I'm writing a very simple program. Im supposed to create an object that contains an array in it.
    For instance:


    calss intArr
    {
    public:
    intArr(int size);
    int operator [] (int index);
    int * Arr;

    private:
    int size

    }

    the point of this small exercise is to be able to do the following:


    void main()

    intArr mainArr(10);

    mainArr[3]=7;


    the idea is to be able to refer to a specific element just like any other built in type array, instead of having to write: mainArr.Arr[3]=7



    However, whenever I perform this assignment I get a compilation error:
    left operand must be l-value.
    When I write the following : i=mainArr[3] there is no problem.


    Here's my operator [] implementation :


    int intArr operator [] (int index)
    {

    return Arr[index]
    }



    what am I doing wrong here?

    thanks

    Gozlan
    And if you get no joy from music hall
    Remember there is always alcohol
    And If you get no joy from Gin
    Here is the abyss jump in

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Lots of typos for a start..

    Also, operator[] was not shown to be a member of intArr in your code.....also if you want to assign to the array like that, then you need to return a reference....not a normal int. Also, you dont show to be managing memory.....

    Here's a rather rushed example;

    Code:
    class intArr
    {
    public:
    	intArr(int size);
    	~intArr(){delete[] Arr;}
    	int& operator [] (int index);
    
    private:
    	int size;
    	int * Arr;
    
    };
    
    intArr::intArr(int sz){
    	Arr = new int[sz];
    	if(!Arr){/*Error management here*/}
    }
    
    int& intArr::operator [] (int index)
    {
    	return Arr[index];
    }
    
    
    int main(){
    
    	intArr mainArr(10);
    
    	mainArr[3] = 7;
    
    	std::cout << mainArr[3];
    
    }

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    24
    Thanks Fordy!

    It did the trick...


    cheers

    gozlan
    And if you get no joy from music hall
    Remember there is always alcohol
    And If you get no joy from Gin
    Here is the abyss jump in

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM