Return more than one answer for midpoint in a Point Class

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 826 Views - Last Post: 19 April 2011 - 05:57 AM Rate Topic: -----

#1 clickspiker233  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 17-April 11

Return more than one answer for midpoint in a Point Class

Posted 19 April 2011 - 02:40 AM

I am having trouble on figureing out on how to return more than one value for x and Y. I know you can only have one return statement but i need to return a value for x and y. I am a little lost on how to do this.

This is what i have so far.


Point.cpp

Point Point::midpoint(Point &midPointX, Point &midPointY)
{

    Point middle;

     

    midPointX = ((0 + middle.getX()) / 2);       // i am adding 0 because its supposed to go from point (0,0)
    
    midPointY = ((0 + middle.getY()) / 2);       // i am adding 0 because its supposed to go from point (0,0)

 

    return middle;
}




here is a sample output of the program. If more code is needed i to see if i am going wrong somewhere i can post it but when i comment out the midpoint stuff it compiles fine and just gives (0,0) as the midpoint



Please enter two coordinates
1st coordinate: 2
2nd coordinate: 2

The coordinates are: (2, 2)

The distance from invoking point (0,0)
to the coordinate you entered (2,2)
is: 2.82843 units

The midpoint between the invoking point (0,0)
and the coordinate you entered (2,2)
is: (0,0)

Is This A Good Question/Topic? 0
  • +

Replies To: Return more than one answer for midpoint in a Point Class

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Return more than one answer for midpoint in a Point Class

Posted 19 April 2011 - 02:54 AM

If you are passing by reference (i.e. prefixing & to the variable name) then the term return is the wrong word to use.

If you pass by reference then the changes you make to those variables will be available outside the function.

We need to see the code that calls this class method.
Was This Post Helpful? 0
  • +
  • -

#3 clickspiker233  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 17-April 11

Re: Return more than one answer for midpoint in a Point Class

Posted 19 April 2011 - 03:02 AM


cout << "The midpoint between the invoking point (" << invokeX << "," << invokeY << ")" << endl;
cout << "and the coordinate you entered (" << pt1.getX() << "," << pt1.getY() << ")" << endl;
cout << "is: (" << pt1.midpoint() << "," << pt1.midpoint() << ")" << endl;// <---- i think your asking about this line? 
return 0;



its the line above the <---- it jumped to the next line when i posted

This post has been edited by clickspiker233: 19 April 2011 - 03:07 AM

Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Return more than one answer for midpoint in a Point Class

Posted 19 April 2011 - 03:08 AM

So you have a method that expects to be passed 2 arguments and you pass it zero arguments?

Do you think that might be a problem when said out loud like that?
Was This Post Helpful? 1
  • +
  • -

#5 clickspiker233  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 17-April 11

Re: Return more than one answer for midpoint in a Point Class

Posted 19 April 2011 - 03:17 AM

View Postjanotte, on 19 April 2011 - 03:08 AM, said:

So you have a method that expects to be passed 2 arguments and you pass it zero arguments?

Do you think that might be a problem when said out loud like that?



:oops:

maybe just a slight one, lol
Was This Post Helpful? 1
  • +
  • -

#6 TMKCodes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 48
  • View blog
  • Posts: 440
  • Joined: 21-March 09

Re: Return more than one answer for midpoint in a Point Class

Posted 19 April 2011 - 03:21 AM

You should call that midPoint function before printing and actually print the midPointX and midPointY variables, because midPoint will not return them, but actually modify them outside of the function.

This post has been edited by TMKCodes: 19 April 2011 - 03:22 AM

Was This Post Helpful? 1
  • +
  • -

#7 clickspiker233  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 17-April 11

Re: Return more than one answer for midpoint in a Point Class

Posted 19 April 2011 - 03:24 AM

View PostTMKCodes, on 19 April 2011 - 03:21 AM, said:

You should call that midPoint function before printing and actually print the midPointX and midPointY variables, because midPoint will not return them, but actually modify them outside of the function.



alright. i think i was about to ask something like that because i keep getting errors
and i have a

 double midPointX, midPointY


so now i need to set the x equal to something and the y equal to something

im not sure how to do this but ill mess with it a little. i know i need to pass the
midpoint the numbers individually but when i do it says

main.cpp:49: error: no matching function for call to 'Point::midpoint(double&)'
Point.h:35: note: candidates are: Point Point::midpoint(Point&)
main.cpp:49: error: no matching function for call to 'Point::midpoint(double&)'
Point.h:35: note: candidates are: Point Point::midpoint(Point&)

This post has been edited by clickspiker233: 19 April 2011 - 03:28 AM

Was This Post Helpful? 0
  • +
  • -

#8 clickspiker233  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 17-April 11

Re: Return more than one answer for midpoint in a Point Class

Posted 19 April 2011 - 03:44 AM

so i keep getting errors saying that there is no matching call to the

main.cpp:49: error: no matching function for call to 'Point::midpoint(double&)'
Point.h:35: note: candidates are: Point Point::midpoint(Point&)
main.cpp:50: error: no matching function for call to 'Point::midpoint(double&)'
Point.h:35: note: candidates are: Point Point::midpoint(Point&)
main.cpp:60: error: no matching function for call to 'Point::midpoint()'
Point.h:35: note: candidates are: Point Point::midpoint(Point&)
main.cpp:60: error: no matching function for call to 'Point::midpoint()'
Point.h:35: note: candidates are: Point Point::midpoint(Point&)

Was This Post Helpful? 0
  • +
  • -

#9 TMKCodes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 48
  • View blog
  • Posts: 440
  • Joined: 21-March 09

Re: Return more than one answer for midpoint in a Point Class

Posted 19 April 2011 - 03:46 AM

pt1.midpoint(midPointX, midPointY);


Was This Post Helpful? 0
  • +
  • -

#10 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Return more than one answer for midpoint in a Point Class

Posted 19 April 2011 - 03:53 AM

To, almost, repeat myself.

You have a method that expects to be passed 2 arguments and you pass it 1 argument?

Do you think that might be a problem when said out loud like that?
Was This Post Helpful? 0
  • +
  • -

#11 clickspiker233  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 17-April 11

Re: Return more than one answer for midpoint in a Point Class

Posted 19 April 2011 - 04:01 AM

View PostTMKCodes, on 19 April 2011 - 03:46 AM, said:

pt1.midpoint(midPointX, midPointY);




ok. i did that and i must have overlooked something simple. (at least im down to one error now)

this is what is giving the error

pt1.midpoint(midpointX, midpointY);


this is where i set it to that


    midpointX = number1; // the X value the user input
    midpointY = number2; // the Y value the user input

    pt1.midpoint(midpointX, midpointY);



This is the error:

main.cpp:52: error: no matching function for call to 'Point::midpoint(double&, double&)'
Point.h:35: note: candidates are: Point Point::midpoint(Point&, Point&)


here is where i output it


cout << "The midpoint between the invoking point (" << invokeX << "," << invokeY << ")" << endl;
cout << "and the coordinate you entered (" << pt1.getX() << "," << pt1.getY() << ")" << endl;
cout << "is: (" << midpointX << "," << midpointY << ")" << endl; // <----- output here





Was This Post Helpful? 0
  • +
  • -

#12 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Return more than one answer for midpoint in a Point Class

Posted 19 April 2011 - 04:04 AM

Do you have a class called Point?
Did you declare midpointX to be an instance of that class? If not, why not?

EDIT
I need to be clearer. The above is not a rhetorical question. You do need to think about what datatype midpointX is and should be.

This post has been edited by janotte: 19 April 2011 - 04:06 AM

Was This Post Helpful? 0
  • +
  • -

#13 clickspiker233  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 17-April 11

Re: Return more than one answer for midpoint in a Point Class

Posted 19 April 2011 - 04:09 AM

yes.

class Point
{
public:
    


// many other declarations


    Point midpoint(Point &midPointX, Point &midPointY);

private:
    double _x;
    double _y;
};


This post has been edited by clickspiker233: 19 April 2011 - 04:13 AM

Was This Post Helpful? 0
  • +
  • -

#14 clickspiker233  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 17-April 11

Re: Return more than one answer for midpoint in a Point Class

Posted 19 April 2011 - 04:25 AM

Which is why im confused as to why im getting an error. it should work. but it doesnt
Was This Post Helpful? 0
  • +
  • -

#15 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Return more than one answer for midpoint in a Point Class

Posted 19 April 2011 - 04:34 AM

So is midpointX an instance of the class Point?
Or is midpointX a private member variable of an instance of that class?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2