Issues overloading [] operator
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Issues overloading [] operator

  1. #1
    Join Date
    Jan 2009
    Posts
    16

    Issues overloading [] operator

    Hey guys,

    I can't seem to get the declaration for overloading the [] operator correctly.

    Code:
    friend const int& operator[] (unsigned int argInt);
    Code:
    const int& operator[] (unsigned int argInt){
            if (getDegree() > argInt){
    		return 0;
    	}else{
    		for (int i = 0; i < nCoeff; ++i){
    			if (degrees[i] == argInt){
    				return coeff[i];
    			}
    		}
    	}
    }
    [edit] I also tried const int& Polynomial:perator[] but it gave me an error saying 'binary '[' : 'Polynomial' does not define this operator or a conversion to a type acceptable to the predefined operator'

    First off my syntax for overloading this operator is incorrect but I do not know where.

    Also it can't find getDegree() nCoeff, degrees and coeff - these are variables (and one function) that are available in the class but because I have no reference to the object - I don't know how to access the object.

    When overloading other operators ++ I have access to the object - is there a way I can access the object in the [] operator?

    Thanks in advance,
    Lang
    Last edited by Lang; February 28th, 2009 at 03:32 PM.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Issues overloading [] operator

    I also tried const int& Polynomial:perator[] but it gave me an error saying 'binary '[' : 'Polynomial' does not define this operator or a conversion to a type acceptable to the predefined operator'
    What is "Polynomial"?

    Please post the exact code that duplicates the error.

    http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

    Quote Originally Posted by Lang View Post
    I can't seem to get the declaration for overloading the [] operator correctly.
    First, there should be two overloads of operator [] -- one that's const and one that isn't .

    http://www.parashift.com/c++-faq-lit...html#faq-18.12
    http://www.parashift.com/c++-faq-lit...erloading.html

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jan 2009
    Posts
    16

    Re: Issues overloading [] operator

    Polynomial is a class I have created.

    Thanks for the links! I'm sure they'll be helpful.

    Polynomial is represented by an array of integers in variable coeff - when someone calls
    Polynomial[index] it will return the integer associated with x^index - so the return type is int

    Code:
    const int operator[] (unsigned index){
    	if (getDegree() > argInt){
    		return 0;
    	}else{
    		for (int i = 0; i < nCoeff; ++i){
    			if (degrees[i] == argInt){
    				return coeff[i];
    			}
    		}
    	}
    }
    This is the same code as in my original post - because its the code that generates the error.

    I get 'operator []' must be a non-static member' as the error

    Thanks,
    Lang
    Last edited by Lang; February 28th, 2009 at 08:22 PM.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Issues overloading [] operator

    Quote Originally Posted by Lang View Post
    Polynomial is a class I have created.
    I know you created it. The point of me asking and giving you the "how to post" link is that there is no code posted that shows this class.

    When you get a compiler error, it is required you show the code that is in error in its full context. This way, all we have to do is copy the code into our compiler, compile it, see the error ourselves, and give you the answer.

    If it's simple enough, maybe we can spot the error without compiling. But a compiler error that tells us about classes we can't see is not helpful.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jan 2009
    Posts
    16

    Re: Issues overloading [] operator

    EDIT: Ok I figured it out - I called (*poly2)[1] -- now all that remains is how to fix my class so that calling poly[2] works correctly

    Ok, here is class Polynomial with all 3 of its constructors. This file is classes.h

    Code:
    class Polynomial{
    	int *coeff; //array of coefficients
    	int nCoeff;
    
    public:
    	//Polynomial with constant
    	Polynomial(const int a = 0);
    	//Polynomial degree n, array(n + 1)
    	Polynomial(const int, int *);
    	//copy constructor
    	Polynomial(const Polynomial &);
    	//deconstructor
    	~Polynomial();
    
    	//getDegree returns degree of polynomial
    	const int getDegree();
    	
    	//overloading array operator
    	int operator[](unsigned int index);
    };
    
    /*
    		CONSTRUCTORS
    */
    Polynomial::Polynomial(const int a){
    	coeff = new int[1];
    	coeff[0] = a;
    	nCoeff = 1;
    }
    
    Polynomial::Polynomial(const int a, int *c){
    	nCoeff = a + 1;
    	coeff = new int[a+1];
    
    	for (int i = 0; i < (a+1); ++i){
    		coeff[i] = c[i];
    	}
    }
    
    Polynomial::Polynomial(const Polynomial &p){
    	//they're not the same
    	if (this != &p){
    		delete []coeff;
    		coeff = p.coeff;
    		nCoeff = p.nCoeff;
    	}
    }
    
    
    /*
    		GETDEGREE()
    */
    const int Polynomial::getDegree(){
    	int i = nCoeff - 1;
    	while (coeff[i] == 0) --i;
    	return i;
    }
    
    //array operator
    int Polynomial::operator[](unsigned int index){
    	return coeff[index];
    }
    And here is the main procedure for testing:

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <conio.h>
    #include "classes.h"
    
    using namespace std;
    
    int main(){
    	int coeff[4] = {1, 3};
    	int coefft[4] = {6, 2};
    	Polynomial * poly = new Polynomial(1, coefft);	
    	Polynomial * poly2 = new Polynomial(1, coeff);
    	cout << *poly2[1]; //*** line
    	delete poly;
    	delete poly2;
    	return 0;
    }
    *** line gives me an error of illegal indirection - the issue I'm having is if I take the * out of *poly[1] I get an access violation when getDegree() is called - as a matter of fact I have no idea why getDegree is even being called!
    Last edited by Lang; March 4th, 2009 at 10:01 AM.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,273

    Re: Issues overloading [] operator

    Try:
    Code:
    cout << (*poly2)[1];
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center