C++ Overloading the divide operator

If a class name is DataContainer how should the function definition be to over load the divide operator / as shown in main ? The goal is to divide each array item by a constant value in main. Thanks for any help or advice on this problem.


DataContainer operator /(const dataContainer& otherArray);//function prototype

DataContainer operator/(const DataContainer & otherArray)//function def
{

}

void main()
{
DataContainer d1,d2;

d1 = d2/5;
}

Last edited on
I don't know what your DataContainer looks like, but possibly like this:

1
2
3
4
5
6
DataContainer operator/(DataContainer arr, int value)
{
    for (size_t i = 0; i < arr.size(); ++i)
        arr[i] += value;
    return arr;
}

Since it needs to pass back a new DataContainer you may as well pass a copy in, add the value and pass that back.

Or maybe like this, if you also want a /=.

1
2
3
4
5
6
7
8
9
10
11
12
// member function
DataContainer& DataContainer::operator/=(int value)
{
    for (size_t i = 0; i < arr.size(); ++i)
        data[i] += value; // assuming contained array is called data
    return *this;
}

DataContainer operator/(DataContainer container, int value)
{
    return container += value;
}

Last edited on
@dutch,

Did you mean to use += in lines 5 and 11?

I think you meant /=.
The first function works, but the second one doesn't because the compiler only allows one argument for overloading operators.

This one compiles with no errors, but does not divide each number by the constant in main. It does put all the values of each item in the array on the right into the values on the left, ie
DataContainer d1, d2 then d1 = d2:

1
2
3
4
5
6
DataContainer& DataContainer::operator/=(int value)
{
    for (size_t i = 0; i < arr.size(); ++i)
        data[i] += value; // assuming contained array is called data
    return *this;
}

I get an error with two arguments:
1
2
3
4
DataContainer operator/(DataContainer container, int value)
{
    return container += value;
}


I am using Visual Studio 2015. Could this be the reason I am getting an error for more than one argument passed into the function?
Thanks for your help.
Define the operator / operator outside of the class itself to let it have two arguments.

If it's defined as part of the class, then the only argument should be the (int value).

This one compiles with no errors, but does not divide each number by the constant in main
Look at what the code is doing. Don't just copy the code without understanding it. As doug4 mentioned, I believe you want to be doing division, not addition.
Last edited on
You're correct. I understand now. thank you. The code below works.

1
2
3
4
5
6
7
8
9
DataContainer &DataContainer::operator /(int value)
{
	for (int i = 0; i < size; i++)
	{
		stack[i] = stack[i] / value;
	}

	return *this;
}


I can also use /= if I overload the compound operators. Thank you for your help.
The code below works.


Does it?

What happens in this situation?

1
2
3
4
5
DataContatiner d;
// populate d with values

DataContainer d1 = d / 2;


What does d contain now?
Topic archived. No new replies allowed.