0

Does anyone know if there's a way to pass a variable that i have created inside of a method to another method . I am posting part of the code so that it is clearer what i need to do

int Point::trovaLato(Point *spigolo2, Point *sol_p, Point *pvet){
//code
//code
Point* lato=new Point(myvalue1,myvalue2,myvalue3);
return 0;
}

Now i want to use that variable 'lato' inside of

int Rettangolo::interseca(Point *sol_p, Point *pvet){
int ritlat, test;
test = punti[0]->trovaLato(punti[1], sol_p, pvet); //this works
if(test){
    test = punti[1]->trovaLato(punti[2], sol_p, pvet);//ok
    if(!test){
        ritlat = lato->intersecaLato(punti[1], punti[2]); //doesn't know what lato is ofcourse :(
//more code
}

Thanks a lot

1 Answer 1

1

Change the return type to Point, and return one:

Point Point::trovaLato(Point* spigolo2, Point* sol_p, Point* pvet){
  //code
  //code
  return Point(myvalue1, myvalue2, myvalue3);
}

If you really need to return an int, you can return an std::pair<int, Point>.

Sign up to request clarification or add additional context in comments.

8 Comments

I put 0 or 1 as return to tell me if the function found the intersection point or it didn't (which is what it does)
You can return a sentinel Point value to indicate failure. Or an optional object. There are different approaches to solve this.
the content of lato could be anything so i wouldn't know what to use as sentinel I have never used std::pair but looks like it could work. It lets me return different type. but what do i write? like return std::make_pair(0, lato); ?
@squirrel Right. Then check thr pair's fist, and use it's second if the first indicates an intersection. But maybe use a bool instead of an int.
well it gives me an error in the header file: 'pair' in namespace 'std' does not name a type
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.