Overload operator

This is a discussion on Overload operator within the C++ Programming forums, part of the General Programming Boards category; Can somebody gimme a hand with this. I really have no clue why this thing is throwin errors. I don't ...

  1. #1
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101

    Overload operator

    Can somebody gimme a hand with this. I really have no clue why this thing is throwin errors. I don't know if I just don't get it or if I put something out of place....

    my declaration is as so:
    Code:
    IntArray operator+(const IntArray& rhs);
    Here's the function:

    Code:
    //sum of two arrays to be assigned to a third
    IntArray IntArray::operator+(const IntArray& rhs)
    {
    
    	
    	////////////////////////////////////////////
    	//         real code
    	//////////////////////////////////////////
    	
    	
    	int  i, j, k;
    
    	//TODO:Reverse this check
    //	if (size1 != size1)
    //		return NULL;
    
    	if(rhs.size() != size())
    	{
    		cout<<"Arrays are not the same size" << endl;
    	}
    	else
    	{
    		m_size = rhs.size();	
    
    		temp = new int[m_size];
    	
    		//TODO: change this size - check off by one.... see operator=...
    		for(i=temp.low(), j = low(), k = rhs.low(); i <= high(); i++, j++, k++)
    		{
    			temp[i] = m_array[j] + rhs[k];
    		}
    		return temp;
    	}
    }
    I'm getting the following errors: c:\documents and settings\hp_administrator\my documents\visual studio 2005\projects\overloadlab\intarray.cpp(106) : error C2511: 'IntArray IntArray:perator +(IntArray)' : overloaded member function not found in 'IntArray'
    c:\documents and settings\hp_administrator\my documents\visual studio 2005\projects\overloadlab\intarray.h(4) : see declaration of 'IntArray'
    Last edited by verbity; 03-30-2007 at 12:40 AM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    LOL. Familiar classname. Familiar error. Guess there's more than one student on here in the same class since I just helped with this assignment earlier in another topic recently.

    Please tell me why you have a prototype like this:

    Code:
    IntArray operator+(const IntArray& rhs);
    And then you go ahead and make a function with a different prototype:

    Code:
    IntArray IntArray::operator+(IntArray rhs)
    Hint: Check the bold text.

  3. #3
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    Sorry man I was just getting frustrated and pulling things on and off and forgot to put it back on. Our entire class is stumped on this assignment. wish I had something more exciting for you but its just me...the student.

    prototype should be:

    Code:
    IntArray IntArray::operator+(const IntArray& rhs)
    Last edited by verbity; 03-30-2007 at 12:21 AM. Reason: screwed up

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    OK, so then when you implement the function, you should declare is with the same arguments.

    ie.... prototype:

    Code:
    IntArray IntArray::operator+(IntArray& rhs)
    Implemented function:

    Code:
    IntArray IntArray::operator+(IntArray rhs)
    If you think these are the same, you are terribly mistaken. One is taking in a reference to an IntArray, and one is taking in a copy of an IntArray. You should prefer the former, and declaring it as const is not a bad idea if you don't have to alter it.

  5. #5
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    I realize that just made a mistake when sending it. So your suggestion is for a implementation with no reference just passing in a copy and I would probably declare as a const as I don't need to change it. On other operators such as += I would need the reference. I get that...that version..the latter is the one I got the least errors on but the problem lay in my for loop to add each of the two values at a given index together and assign to the temp array at the same relative index. Just a side note I have an overloaded [] in my code as well that simply checks for proper range as well. And I'm just trying to wrap my head around the concept.....don't or would prefer no code as I like to do that on my own.....
    Last edited by verbity; 03-30-2007 at 12:40 AM.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by verbity View Post
    So your suggestion is for a implementation with no reference just passing in a copy and I would probably declare as a const as I don't need to change it.
    Yikes! Don't mangle my advice. LOL.

    No, I think you should be passing a const reference. That's the most efficient and logical way to do it if your prototype is that way.

    What I was pointing out what was that your declaration had it taking a reference but your implementation had it taking a copy.

    Quote Originally Posted by verbity View Post
    And I'm just trying to wrap my head around the concept.....don't or would prefer no code as I like to do that on my own.....
    I understand this forum is known to your classmates and that there's reportedly been some cases of plagiarism. Guard your code.
    Last edited by MacGyver; 03-30-2007 at 12:57 AM.

  7. #7
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    Ok I misunderstood you. I just broke out the book and class notes to 'bone up' on my references. I still am not sure why I would be getting an error on something as straightforward as that for loop. And I believe our professor monitors this site so if someone steals my stuff they'll get busted. I get annoyed, probably just as much as you guys that know what you're doing, when people just say I have this problem and can you help. Or when the sorta do a bit on the assignment and send the code in and then someone finishes it for them.....meanwhile I'm pulling my hair out (even though I have none) and spending 30 hours on each project.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I suppose for the purpose of adding it doesn't matter if you pass it a reference or not, but as I said in the other topic, I don't use C++ references much.

    One major problem with your loop is that you call temp.low(). You can't do that because temp is of type int * I believe.

  9. #9
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    Sorry if I'm turning into a pain in the ass. originally I had it as
    Code:
    IntArray temp(someSize);
    But it didn't like that either. I have 4 different constructors for IntArray...one that takes no parameters, one that takes 1 param 0 to that int, one that takes two size1 to size 2, and one that takes another IntArray object.

    I'm about as confused as at anytime.... I was trying to use the this pointer to get the side of the lhs but it didn't like that either so I started using two parameters....althought the call looks like this:

    Code:
    IntArray c = a + b;
    I'll just keep pluggin along hopefully it'll wack me in the face like it usually does.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Often operator+ is implemented as a non-member function, but it can be implemented as a member function as well. It looks like you are implementing it as a member function. In that case, the best prototype would be:
    Code:
    IntArray operator+(const IntArray& rhs) const;
    Have you implemented operator+=? If so, you can just use that to do all the your work in your operator+. Create a temporary IntArray from *this, then use += to add rhs to the temp. Then return the temp and you are done.

    If you continue with your current setup. Just take each line of code separately and figure out what it is doing. For IntArray temp(someSize), you are creating an object of type IntArray and passing an integer size to the constructor. Do you have a constructor that takes a size? Is that what you were trying to do? If so, and it gives you an error, then post the relevant code and someone should be able to help you with it.

  11. #11
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    I do have a += operator but I believe we're not supposed to use that one because in IntArray c = a + b we can't change a. This is where I'm at now...which isn't much further than last time:

    Code:
    IntArray IntArray::operator+(const IntArray& rhs)const
    {
    
    	
    	////////////////////////////////////////////
    	//         real code
    	//////////////////////////////////////////
    	
    	
    	int  i, j, k;
    
    	
    	if(rhs.size() != this->size())
    	{
    		cout<<"Arrays are not the same size" << endl;
    	}
    	else
    	{
    		IntArray temp = new IntArray(rhs.size());
    	
    		//TODO: change this size - check off by one.... see operator=...
    		for(i=temp.low(), j = this->low(), k = rhs.low(); i <= high(); i++, j++, k++)
    		{
    			temp[i] = this[j] + rhs[k];
    		}
    		
    		return temp;
    	}
    }
    This is the error I'm getting all over the place:

    c:\documents and settings\hp_administrator\my documents\visual studio 2005\projects\overloadlab\intarray.cpp(119) : error C2662: 'IntArray::size' : cannot convert 'this' pointer from 'const IntArray' to 'IntArray &'


    And as I said earlier I have a [] overload that checks for proper range which is giving me this error:

    c:\documents and settings\hp_administrator\my documents\visual studio 2005\projects\overloadlab\intarray.cpp(130) : error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const IntArray' (or there is no acceptable conversion)
    c:\documents and settings\hp_administrator\my documents\visual studio 2005\projects\overloadlab\intarray.h(20): could be 'int IntArray:perator [](int)'
    while trying to match the argument list '(const IntArray, int)'
    Last edited by verbity; 03-30-2007 at 01:51 AM.

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I do have a += operator but I believe we're not supposed to use that one because in IntArray c = a + b we can't change a.
    I believe you won't need to change a. Create a temporary IntArray from a, using the copy constructor. Then return temp += b;

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    >> cannot convert 'this' pointer from 'const IntArray' to 'IntArray &'
    This error occurs when you try to change the this IntArray. As you know, you don't want to change a in c = a + b, and that error is telling you that you are changing a. In reality, you might not actually be changing it, but you might be calling a function that doesn't guarantee that it won't change. You should have two operator[] functions that look like this:
    Code:
    const int& operator[](int index) const;
    int& operator[](int index);
    The one that is const will be used because it guarantees it won't change a. Also, make sure your other functions are const that should be const (like size(), low() and high()).

    There are several other errors in that code, like this[j] won't work because this is a pointer (use (*this)[j]). Also, IntArray temp = new IntArray(rhs.size()); is wrong because temp is not a pointer. You could just use IntArray temp(rhs.size());

    Finally, as anon says you can use operator+= in your implementation of operator+ without changing a. Do this by making a temporary copy of a and calling operator+= with that.

  14. #14
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    What are your array names in the objects? You can try and see if this works.

    Code:
    for(i = 0; i < size(); i++)
    {
    	temp.array[i] = array[i] + rhs.array[i];
    }
    Then you don't have to worry about calling the overloaded [] operator. If you do it this way you would just use i = 0 since each array is the same size and you're spanning from 0 to 1 less than the size.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Failure to overload operator delete
    By Elysia in forum C++ Programming
    Replies: 16
    Last Post: 07-10-2008, 01:23 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Addition Operator Overload
    By DarkDot in forum C++ Programming
    Replies: 2
    Last Post: 05-04-2007, 03:43 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21