簡體   English   中英

帶有構造函數和 function 的類中有很多錯誤

[英]A lot of errors in classes with constructors and function

這是我的文件的代碼

#include <iostream>
 #include <string>
 #include <cstdio>
 #include <fstream>



 class Sphere{
   private:
     int x;
     int y;
     int r;
   public:
     Sphere();
     void set_x(int first);
     void set_y(int second);
     void set_r(int radius);
     int get_x();
     int get_y();
     int get_r();
 };

 class Array{
   private:
     Sphere** spheres;
     int size;
     int maxsize=101;
   public:
     Array();
     ~Array();
     void addSphere(const Sphere& sphere);
     Sphere& getSphere(int index)const;
     int getQuant(int xp,int yp);
 };

int main(){
  Array spheres;
  for(int i=0;i<101;i++){
    spheres.addSphere(Sphere());
  }
  getQuant(15,30);
  std::cout << "The programm made it to the end" << std::endl;
  return 0;
}

Sphere::Sphere(){
  x=rand()%100;
  y=rand()%100;
  r=rand()%50;
}


void Sphere::set_x(int first){
  x=first;
}
void Sphere::set_y(int second){
  y=second;
}
void Sphere::set_r(int radius){
  r=radius;
}
int Sphere::get_x(){
  return x;
}
int Sphere::get_y(){
  return y;
}
int Sphere::get_r(){
  return r;
}

Array::Array():size(0){
  spheres=new Sphere*[maxsize];
}

Array::~Array(){
  for(int i=0;i<maxsize;i++){
    delete spheres[i];
  }
  delete[] spheres;
}


void Array::addSphere( const Sphere& sphere){
  if(size<maxsize){
    spheres[size]=new Sphere();
    size++;
  }else{
    std::cout << "\nThe limit is exceeded at" << size << std::endl;
  }
}
int Array::getQuant(int xp,int yp){
  int quantity;
  for(int i=0;i<101;i++){
    getSphere(i);
    if(this->x <= xp){
      if(this->y <=yp){
        quant++;
      }
    }
  }
  std:cout << "The quantity of spheres that has centers inside the stated area is:" << quantity;
}

Sphere& Array::getSphere(int index)const{
  return *spheres[index];
}

好吧,這個程序應該以下列方式工作:生成 101 個球體(這不是球體,但沒關系),中心坐標和半徑是隨機數。 然后它應該找到在某個坐標區域中具有中心的球體的數量。 但我有很多錯誤。 這是他們的文字。

./src/main.cpp:42:3: error: use of undeclared identifier 'getQuant'
  getQuant(15,30);
  ^
./src/main.cpp:97:14: error: no member named 'x' in 'Array'
    if(this->x <= xp){
       ~~~~  ^
./src/main.cpp:98:16: error: no member named 'y' in 'Array'
      if(this->y <=yp){
         ~~~~  ^
./src/main.cpp:99:9: error: use of undeclared identifier 'quant'
        quant++;
        ^
./src/main.cpp:103:7: error: use of undeclared identifier 'cout'; did you mean 'std::cout'?
  std:cout << "The quantity of spheres that has centers inside the stated area is:" << quantity;
      ^~~~
      std::cout

你能幫我解決嗎?

只需遵循每個錯誤:

./src/main.cpp:42:3:錯誤:使用未聲明的標識符“getQuant”getQuant(15,30);

在 main() function 中,您調用getQuant ,它是Array class 的一部分,但您將其稱為獨立的 function(它不會退出)。

你可能想要:

spheres->getQuant(15,30);

./src/main.cpp:97:14: 錯誤:'Array' 中沒有名為'x'的成員 if(this->x <= xp){

getQuant實現中,您有this->xArray沒有x成員。


./src/main.cpp:99:9:錯誤:使用未聲明的標識符“quant”quant++;

getQuant實現中,您有quant++但未聲明此變量。 你是說quantity嗎?


./src/main.cpp:103:7:錯誤:使用未聲明的標識符“cout”; 你的意思是'std :: cout'嗎?

cout<iostream>的一部分,並帶有std名稱規范。 您必須調用std::coutusing std::cout;

以下是原始代碼中的錯誤:

  1. 在主 function 中,您沒有使用 object 調用該方法 getQuant()。 您需要使用 object 調用 getQuant() 方法來修復該錯誤。

  2. 方法 Array::getQuant() 中的第一個錯誤

要將 x 與 xp 進行比較,您需要調用getSphere(i).get_x() <= xp ,因為您的數組class 不包含數據成員 x,並且只有Sphere class 包含數據成員 x。 因此,您需要通過 class Sphere的 object 訪問數據成員 x。

  1. 方法 Array::getQuant() 中的第二個錯誤

    您編寫了“quant++” ,但quant是在任何地方定義的。 我認為你打錯了,應該輸入“數量++”

  2. 方法 Array::getQuant() 中的第三個錯誤

當您在該方法的末尾編寫std:cout時,您犯了一個錯字。 正確的語法是std::cout

=========

下面是我修復了所有錯誤並確保代碼編譯和運行良好的新代碼

int Array::getQuant(int xp,int yp){
  int quantity;
  for(int i=0;i<101;i++){
    //getSphere(i);
    if(getSphere(i).get_x() <= xp){
      if(getSphere(i).get_y() <=yp){
        quantity++;
      }
    }
  }
  std:cout << "The quantity of spheres that has centers inside the stated area is:" << quantity;
}

==========

int main(){
  Array spheres;
  for(int i=0;i<101;i++){
    spheres.addSphere(Sphere());
  }
  spheres.getQuant(15,30); // I call getQuant() with an object to fix the error
  std::cout << "The programm made it to the end" << std::endl;
  return 0;
}

==============

同樣,上面這兩種方法已得到修復並正確運行,正如我已經驗證的那樣。 如果您遇到任何問題,請告訴我。

暫無
暫無

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

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