I was just wondering how to pass two objects s1 and s2, both of which have to arguments, to a function, someFunction(). I actually only want to pass the second argument of each object for s1 and s2 to the same function, compare the two arguments and then display some sort of output. Is there a way to do this easily?
(

class SomeObject
{
	private:
		int a,
		    b;
	public:
		int someFunction(SomeObject & other);
}

int SomeObject::someFunction(SomeObject & other)
{
     //trying to compare s1's second argument 2 and s2's second argument 4
     //and return their comparison (not as a bool) as int b and other.b, private data
     //member
}

int main()
{		
     SomeObject s1(1,2), s2(3,4);


return 0;
}

Recommended Answers

All 11 Replies

I think you need to create the proper class definition first. Your initialization statement(s) on Line 19 don't match your class' constructor definition. I would be surprised if this compiles.

  1. You need a semi-colon after the brace on Line 8.
  2. You have not created the proper constructors. The compiler will provide a "default" compiler for you, but the compiler-provided default is not sufficient for how you are attempting to use the class.
    class someClass {
     public:
       someClass();          //default constructor
       someClass(int, int);  //specified constructor
       someClass(someClass&);//copy constructor
    }; //<---end class definition with semi-colon

    The specified constructor is the one you are attempting to call in your initializations on Line 19, but you don't have one.

Once you have that working properly, you should be able to just call someFunction() from one of the objects and compare the values of their data members.

Edited because I was wrong:
You can call someFunction by s1.someFunction(s2) and have it return whichever value you want, but you will still need to make it a void function if you want both values returned. It would be easier to make a seperate function to access s1's b vaule.

Depending on what you want the function to do, return the highest, lowest, or whatever, you'd just return whichever one you wanted. If you wanted to return both, you would need to use pointers in the function and make it void.

You also need a constructor as already stated.

commented: s1.someFunction(s2) is exactly what I was looking for..Thanks! +1

I'm sorry, that was just a quick example I did and I forgot to put in my constructor. I should have coded it better.

I think you need to create the proper class definition and constructors first. Your initialization statement(s) on Line 19 don't match your class' constructor definition. I would be surprised if this compiles.

Once you have that working properly, you should be able to just call someFunction() from one of the objects and compare the values of their data members.

I agree with Fbody, although you don't even appear to have a constructor to use as you are attempting.

Surely you would need to actually assign the values that you are passing as parameters?

Maybe something more like the following is what you are attempting?

e.g.

class SomeObject
{
    private: 
        int a, b;

    public:
        SomeObject( int value1, int value2 ){ a = value1; b = value2; }
        int someFunction( SomeObject& other );

        int getA() { return a; }
        int getB() { return b; }
};

int SomeObject::someFunction( SomeObject& other )
{
    if( other.getB() > b )
        return other.getB();

    return b;
}

int main()
{
    SomeObject s1(1,2), s2(3,4);
    s1.someFunction( s2 );

    return 0;
}

You could of course overload an operator too.

As your function header is written, you are only passing one SomeObject object to the function. In order to compare two objects, you'll need to pass both objects to the function.

Depending on what you what the function to do, return the highest, lowest, or whatever, you'd just return whichever one you wanted. If you wanted to return both, you would need to use pointers in the function and make it void.

You also need a constructor as already stated.

The way someFunction() is declared should be okay because it is a member function. As a member function, it has access to the private members of both objects. Something like this would be fine:

void SomeObject::someFunction(SomeObject & other)
{
  if ((this->b) > (other.b))
    cout << "This b is greater than other b" << endl;
  else
    cout << "Other b is greater than this b" << endl;
}

int main()
{		
  SomeObject s1(1,2), s2(3,4);
  s1.someFunction(s2);

return 0;
}

@OP:
NOTE, this code is very similar to yours, but it will not work within your program as written. IT IS ONLY AN EXAMPLE.

Ah you have taught me a lesson too then :)
I didn't realise that the member function would have access the private members of both objects. Thanks for the info ;)

Another option would be to overload the comparison operators to work with the SomeObject class. However, this option could get rather messy, from a readability and maintenance perspective, depending on how the rest of the program works and what the class' expected behavior is.

With a data structure like that of SomeObject, it's really not a good idea unless how the comparison acts is somewhat expected or reasonable and extremely well documented.

The way someFunction() is declared should be okay because it is a member function. As a member function, it has access to the private members of both objects. Something like this would be fine:

void SomeObject::someFunction(SomeObject & other)
{
  if ((this->b) > (other.b))
    cout << "This b is greater than other b" << endl;
  else
    cout << "Other b is greater than this b" << endl;
}

int main()
{		
  SomeObject s1(1,2), s2(3,4);
  s1.someFunction(s2);

return 0;
}

@OP:
NOTE, this code is very similar to yours, but it will not work within your program as written. IT IS ONLY AN EXAMPLE.

Would something like this work to return the two values?

:

void SomeObject::someFunction(SomeObject & other, int *firstb, int *secondb)
{
  if ((this->b) > (other.b))
    cout << "This b is greater than other b" << endl;
  else
    cout << "Other b is greater than this b" << endl;
  *firstb = this ->b;
  *secondb = other.b;
}

int main()
{		
  int *valueA, *valueB;
  SomeObject s1(1,2), s2(3,4);
  s1.someFunction(s2, valueA, valueB);

return 0;
}

I'm not sure if the syntax is correct, but I know I've used the concept of passing pointers back to main before. valueA should point to the value in s1.b and valueB should point to s2.b

Your syntax is close, but not quite correct.

What you get is not technically a "return value", but yes, you could use references and/or pointers to extract 2 values from a function.

void SomeObject::someFunction(SomeObject & other, int *firstb, int *secondb)
{
  if ((this->b) > (other.b))
    cout << "This b is greater than other b" << endl;
  else
    cout << "Other b is greater than this b" << endl;
  *firstb = this ->b;  //de-reference firstb
  *secondb = other.b;  //de-reference secondb
}

int main()
{		
  int valueA, valueB;  //declare 2 integers
  SomeObject s1(1,2), s2(3,4);
  s1.someFunction(s2, &valueA, &valueB); //call SomeObject::someFunction(), provide 2 integer references

return 0;
}

Honestly though, I wouldn't recommend doing this. It sort of breaks the encapsulation of SomeObject::a and SomeObject::b.

Thank you so much everyone, I was looking for the function call s1.someFunction(s2) so I could get the LCD of a rational number. Sorry about being a little unclear in my code and what I needed. I just wanted it to be as general as possible so I could figure most of it out myself, since I'm so new to coding. You guys rock!

sorry, reposted twice, thanks again though!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.