Overload Operators

This is a discussion on Overload Operators within the C++ Programming forums, part of the General Programming Boards category; Why would I get errors on something simple like: Code: IntArray::IntArray() with a .h declaration of IntArray()?? And I also ...

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

    Exclamation Overload Operators

    Why would I get errors on something simple like:

    Code:
    IntArray::IntArray() with a .h declaration of IntArray()??
    And I also can't seem to not "redfine" my overload functions
    Code:
    //.h declaration
    IntArray overload=(IntArray x)
    //.cpp function
    IntArray IntArray::overload=(IntArray x)
    {
    }

    I'll post the full cess pool of code below but I figured I'd spare everyone my messing stuff. It seems as though I am not supposed to use Int... cause it says int is assumed...but I thought attached to IntArray it wouldn't matter. And I haven't the slightest clue about the redefinition.....I'm doing it exactly how my instructor did in his examples so....beats me....thanks in advance.

    .h file
    Code:
    #ifndef _INTARRAY_H
    #define _INTARRAY_H
    
    class IntArray{
    private:
    	char* array;
    	int m_high;
    	int m_low;
    
    public:
    	IntArray();						//constructor with no params 0 to 10 indices
    	IntArray(int x);				//constructor with 1 param indices 0 to int x
    	IntArray(int x, int y);			//constructor with 2 params indices from int x to int y, int x !> y
    	IntArray(IntArray x);			//constructor with 1 param which is an Object of type IntArray
    	
    	int high();
    	int low();
    	void operator[](int x, int y);
    	IntArray operator+(IntArray x, IntArray y);
    	IntArray operator=(IntArray x);
    	boolean operator==(IntArray x);
    	boolean operator!=(IntArray x);
    	IntArray operator+=(IntArray x);
    
    #endif

    .cpp file
    Code:
    
    #include <iostream>
    #include "intarray.h"
    
    
    using namespace std;
    IntArray::IntArray()
    {
    	const int size = 10
    	array = new char[size];
    	m_low = 0;
    	m_high = 10;
    }
    
    IntArray::IntArray(int x)
    {
    	const int size = x;
    	array = new char[size];
    	m_low = 0;
    	m_high = size -1;
    }
    
    IntArray::IntArray(int x, int y)
    {
    	const int size = y - x + 1;
    	array = new char[size];
    	m_low = x;
    	m_high = y;
    }
    
    IntArray::IntArray(intarray x)
    {
    	const int size = x.high() - x.low();
    	array = new char[size];
    	m_low = x.low();
    	m_high = x.high();
    }
    
    int IntArray::high()
    {
    	return m_high;
    }
    
    int IntArray::low()
    {
    	return m_low;
    }
    
    //subscript operator overload
    void IntArray::operator[](int x, int y)
    {
    	
    			
    
    	
    }
    
    //sum of two arrays to be assigned to a third
    IntArray IntArray::operator+(IntArray x, IntArray y)
    {
    
    	/*PSEUDO CODE SORTA
    
    	size = ??
       
    
    	for(i = low(); i <= high(); i++)
    	{
    		sumArray[i] = array1[i] + array2[i];
    	}
    
    	return sumArray;
    	
    	Then call the operator=.....on sumArray[i]..
    	*/
    	////////////////////////////////////////////
    	//         real code
    	//////////////////////////////////////////
    	
    	
    	int size1, size2, i, j, k;
    	size2 = y.high() - y.low();
    	size1 = x.high() - x.low();
    
    	if(size1 == size2)
    	{
    		size = size1;
    
    		thisArray = new char[size];
    
    		for(i=thisArray.low, j=y.low(), k=x.low(); i <= thisArray.high(); i++)
    		{
    			thisArray[i] = y[j] + x[k];
    		}
    
    		size2 = thisArray.high() - thisArray.low();
    		size1 = array.high() - array.low();
    
    		if(size1 == size2)
    		{
    
    			for(i=thisArray.low, j=array.low(); j <= array.high(); i++)
    			{
    				array[k]= thisArray[i];
    			}
    
    		}
    		return array;
    	}
    	else
    	{
    	cout<<"Arrays are not the same size" << endl;
    	wait();
    	}
    
    		
    
    }
    
    IntArray IntArray::operator=(IntArray x)
    {
    	/*
    	PSEUDO STUFF
    
    	Find both sizes
    	if size == then
    
    	run a for loop (i = low; i < high; i++)
    	  array[i] = x.array[i];
    
    	return array;
    	*/
    	/////////////////////////////////////////////
    	//        real code
    	/////////////////////////////////////////////
    
    	int size1, size2, i, k;
    	size2 = y.high() - y.low();
    	size1 = array.high() - array.low();
    
    	if(size1 = size2)
    	{
    		for(i=y.low, k=array.low; k <= array.high(); k++, i++)
    		{
    			array[i] = y[k];
    		}
    		return array;
    	}
    	else
    	{
    		cout<<"Arrays are not the same size" << endl;
    		wait();
    	}
    			
    
    }
    
    boolean IntArray::operator==(IntArray x)
    {
    	/*
    	Pseudo Stuff
    
    	//lengths don't have to match
    
    	if array1[i] == array2[2]
    	in a for loop for EXISTING ELEMENTS
    	return true
    	else
    	return false
    	use the smaller size array as condition
    	*/
    
    }
    
    boolean IntArray::operator!=(IntArray x)
    {
    	/*
    	Psuedo Stuff
    
    	//lengths don't have to match
    
    	if array[i]==array2[2]
    	in a for loop for EXISTING ELEMENTS
    	return true
    	else 
    	return false
    	use a smaller size array as condition
    	*/
    }
    
    IntArray IntArray::operator=+(IntArray x)
    {
    	/*
    	Pseudo Stuff
    
    	find both sizes
    	if size== the return false
    	else move on
    
    	if any array1[i] != array2[2[
    	in a for loop for each element
    	return true
    	else return false
    	*/
    }

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Did you perhaps forget this:

    };

    before the #endif?

    edit:
    Code:
    	void operator[](int x, int y);
    	IntArray operator+(IntArray x, IntArray y);
    I've a feeling these can only have one parameter ... I'm not positive about that though. Just a memory in my head.
    Last edited by twomers; 03-25-2007 at 10:44 AM.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Operator + can have two parameters but then it must be a non-member function (I believe this is the preferred way of overloading addition operator.

    Don't forget that the this-pointer is passed as an implicit argument to member functions, making it altogether a third argument in this case.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    You have typos in the posted code, is that how you have it or did you re-type it incorrectly?

    >> IntArray overload=(IntArray x)
    overload should be operator.

    >> IntArray IntArray::operator=+(IntArray x)
    should be += not =+.

    >> boolean operator==(IntArray x);
    There is no such thing as boolean unless you have defined it yourself. You probably meant bool.

    >> void operator[](int x, int y);
    twomers is correct, this should have one parameter. Also it makes no sense to have operator[] without a return value.

    >> IntArray operator+(IntArray x, IntArray y);
    twomers is right about this as well. You can have two parameters if this is a non-member function, but you have it as a member function in which case it should only have one.

    >> IntArray(IntArray x);
    This is not the correct way to have a copy constructor, you need to take the parameter by reference to const, otherwise there will be infinite copies made trying to call the function.


    There are so many errors here that you would be better off starting with something that compiles, and then adding one overloaded operator at a time and getting that to compile and run as you want.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 06:42 PM
  2. using templates to overload operators
    By Ancient Dragon in forum C++ Programming
    Replies: 3
    Last Post: 05-24-2006, 10:18 PM
  3. How do I Overload these operators in this program?
    By advocation in forum C++ Programming
    Replies: 9
    Last Post: 04-24-2005, 11:29 AM
  4. Having trouble with operator*=()
    By Lurker in forum C++ Programming
    Replies: 10
    Last Post: 10-26-2003, 02:03 PM
  5. Replies: 5
    Last Post: 11-24-2002, 10:05 PM

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