Thread: Need help displaying output from function

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    22

    Need help displaying output from function

    Ok, what I want this function to do is generate two arrays, called box, each with 3 numbers and it will tell the user which, if any, of these numbers are equal. However, the biggest problem I am having is just displaying the output. I am only getting one error and this is it
    Code:
    cbox03.cpp(315) : error C2660: 'CBox::operator ==' : function does not take 0 arguments
    Here is the rest of the code for the function
    Code:
    bool CBox::operator ==(CBox b2)
    {	
    	int x=length;
    	int y=width;
    	int z=height;
    
    	if (this->length==b2.length)
    	{
    		cout << "The height of the two boxes are equal";
    	}
    	else if (this->width==b2.width)
    	{
    		cout << "The length of the two boxes are equal";
    	}
    	else if (this->height==b2.height)
    	{
    		cout << "The height of the two boxes are equal";
    	}
    	else if (this->height==b2.height&&this->length==b2.length&&this->width==b2.width)
    	{
    		cout << "All the sides of each box are equal";
    	}
    }
    
    void test_CBox_operatorEqual(void)
      {
      displayHeading("test_CBox_operatorEqual");
      for (int t=1; t<=TEST_COUNT; t++)
        {
            CBox b1('r');
    	CBox b2('r');
    	b1.display();
    	b2.display();
    	cout << CBox::operator ==();
    	cout << endl;
    	}
      }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, that means just what it says. operator == needs a right hand side. What do you want to check to be equal to? You should probably do b1==b2. Note also that you are trying to print the bool output of the ==.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    22
    How would I print out whats in the if else statements then? Sorry for so many questions, it just seems like all the small things are what give me the most trouble in C++. This isn't the first time I've had trouble with something small like this. I'm checking to see if the random values in one array will be equal to the random values in another array.
    Last edited by c++prog; 11-11-2008 at 09:33 PM.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    This is no small thing. There are several conceptual errors:

    1) Operator == is used to compare two objects for equality. Generally it is a pure function that does not change either object or do anything but return the result. It can be useful to print out debug information in such a function, but it is not a function meant for printing. If you want a function that does what your operator == is doing now, I recommend you call it something else.
    2) "bool CBox::operator ==(CBox b2)" is a method returning a bool. Where is the return statement?
    3) operator == is used to compare two objects, in this case CBoxes, for equality. It returns a bool. It can therefore only apply one standard of equality, not four; It should answer the question "Is that CBox equal to that CBox, Yes or No?
    4)CBox::operator ==() is the syntax used to call a static method taking no arguements. Operator == here defined is neither of these.
    5) The purpose of overloading operators is to treat objects like primitive types. In particular it is used to provide the same syntax for doing the same conceptual thing, which in this case is comparing for equality. Therefore if you overload operators, you should then apply those operators in the same way you apply them to primitives. So just as you would write "if(int1 == int2)" for ints, you should use the same syntax for comparing CBox's.
    6) Since CBox::operator ==() is not a state changing function function, running the same test multiple times on it is pointless; the result will always be the same. Instead it might be useful to change the boxes on which it is called on.

    And there's also some small things:
    1) the variables x, y, and z are unused.
    2) Since CBox::operator ==() does not change the state of the object it is called on, it should be declared such, by declaring it as "bool CBox::operator ==(CBox b2) const"
    3) Since CBox::operator ==() does not change the state of the object passed, that object should be passed by const reference, except arguably if CBox is 8 bytes or less. I'd recommend passing a const reference even then.
    Last edited by King Mir; 11-11-2008 at 10:09 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM