简体   繁体   English

C++ BOOL FUNCTION 带 *,你能帮帮我吗? 我的代码有什么问题?

[英]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.问题:考虑一个结构来表示二维空间中的一个点,并实现一个 function 来指示给定点 p 是位于矩形内部还是外部。 The rectangle is defined by its lower left v1 and upper right v2 vertices.矩形由其左下 v1 和右上 v2 顶点定义。 THE function must return true if the point is located inside the rectangle, and false otherwise.如果该点位于矩形内,则 function 必须返回 true,否则返回 false。 This function must obey the following prototype: bool dentroRetangulo(Ponto* v1, Ponto* v2, Ponto* P);这个 function 必须服从以下原型: 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.对于签名bool dentroRetangulo(Ponto* v1, Ponto* v2, Ponto* P) ,您有 3 个指针 arguments。 So you need to use -> to access the data member.所以你需要使用->来访问数据成员。

To pass pointer arguments, you need to use & .要传递指针 arguments,您需要使用&

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 .在线演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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