簡體   English   中英

C++ BOOL FUNCTION 帶 *,你能幫幫我嗎? 我的代碼有什么問題?

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

問題:考慮一個結構來表示二維空間中的一個點,並實現一個 function 來指示給定點 p 是位於矩形內部還是外部。 矩形由其左下 v1 和右上 v2 頂點定義。 如果該點位於矩形內,則 function 必須返回 true,否則返回 false。 這個 function 必須服從以下原型: bool dentroRetangulo(Ponto* v1, Ponto* v2, Ponto* P);

我的代碼:

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;

}

對於簽名bool dentroRetangulo(Ponto* v1, Ponto* v2, Ponto* P) ,您有 3 個指針 arguments。 所以你需要使用->來訪問數據成員。

要傳遞指針 arguments,您需要使用&

現代編譯器的編譯錯誤消息非常清楚,只需按照它們並修復您的代碼即可。

#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;
}

在線演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM