简体   繁体   中英

C++ BOOL FUNCTION WITH * , could you help me? What is wrong with my code?

Question: Consider a structure to represent a point in 2D space and implement a function that indicates whether a given point p is located inside or outside a rectangle. The rectangle is defined by its lower left v1 and upper right v2 vertices. THE function must return true if the point is located inside the rectangle, and false otherwise. This function must obey the following prototype: bool dentroRetangulo(Ponto* v1, Ponto* v2, Ponto* P);

My code:

using namespace std;

struct Ponto{
    int x;
    int y;
};

bool dentroRetangulo(Ponto* v1, Ponto* v2, Ponto* P){
    if ((P.x>=v1.x && P.x<=v2.x)&&(P.y>=v1.y && P.y<=v2.y))
        return true;
    
    return false;
}

int main(){

    Ponto v1,v2,P;
    //int x, y;

    cout << "Insert the X and Y from the vertex of the lower left rectangle: \n";
    cout << "X = ";
    cin >> v1.x;
    cout << "Y = ";
    cin >> v1.y;
    cout << "Insert the X and Y from the vertex of the upper right rectangle: \n";
    cout << "X = ";
    cin >> v2.x;
    cout << "Y = ";
    cin >> v2.y;

    cout << "The coordinates of the rectangle vertices are: "<<"("<<v1.x <<"," <<v1.y<<")"<<"(" <<v2.x<<","<< v2.y<<")"<<endl;
    cout<<"\nEnter the position of the point"<<endl;
    cout<<"X = ";
    cin>>P.x;
    cout<<"Y = ";
    cin>>P.y;

    if (dentroRetangulo(v1, v2, P){}  // Here I am not able to call the function correctly due to the pointer
        cout<<"\nThis Point is inside the Rectangle !"<<endl;
        cout<<"("<<P.x<<")"<<" "<<"("<<P.y<<")"<<endl;
    }else{
        cout<<"This point is outside the rectangle !"<<endl;
    }
    return 0;

}

For the signature bool dentroRetangulo(Ponto* v1, Ponto* v2, Ponto* P) , you have 3 pointer arguments. So you need to use -> to access the data member.

To pass pointer arguments, you need to use & .

The compile error message from modern compiler is very clear, just follow them and fix your code.

#include <ostream>
using namespace std;

struct Ponto {
  int x;
  int y;
};

bool dentroRetangulo(Ponto* v1, Ponto* v2, Ponto* P) {
  if ((P->x >= v1->x && P->x <= v2->x) && (P->y >= v1->y && P->y <= v2->y))
    return true;

  return false;
}

int main() {
  Ponto v1, v2, P;
  // int x, y;

  cout << "Insert the X and Y from the vertex of the lower left rectangle: \n";
  cout << "X = ";
  cin >> v1.x;
  cout << "Y = ";
  cin >> v1.y;
  cout << "Insert the X and Y from the vertex of the upper right rectangle: \n";
  cout << "X = ";
  cin >> v2.x;
  cout << "Y = ";
  cin >> v2.y;

  cout << "The coordinates of the rectangle vertices are: "
       << "(" << v1.x << "," << v1.y << ")"
       << "(" << v2.x << "," << v2.y << ")" << endl;
  cout << "\nEnter the position of the point" << endl;
  cout << "X = ";
  cin >> P.x;
  cout << "Y = ";
  cin >> P.y;

  if (dentroRetangulo(&v1, &v2, &P)) {  // Here I am not able to call the
                    // function correctly due to the pointer
    cout << "\nThis Point is inside the Rectangle !" << endl;
    cout << "(" << P.x << ")"
     << " "
     << "(" << P.y << ")" << endl;
  } else {
    cout << "This point is outside the rectangle !" << endl;
  }
  return 0;
}

Online demo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM