类 构造函数和函数有很多错误

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: error: use of undeclared identifier 'getQuant' getQuant(15,30);

在 main() 函数中,您调用 getQuant,它是 Array class 的一部分,但您将其作为独立函数调用(不会退出)。

你可能想要:

spheres->getQuant(15,30);

./src/main.cpp:97:14: error: no member named 'x' in 'Array' if(this->x <= xp){

getQuant 实现中,您有 this->xArray 没有 x 成员。


./src/main.cpp:99:9: error: use of undeclared identifier 'quant' quant++;

getQuant 实现中,您有 quant++ 但未声明此变量。你是说 quantity 吗?


./src/main.cpp:103:7: error: use of undeclared identifier 'cout'; did you mean 'std::cout'?

cout<iostream> 的一部分,并带有 std namespece。您必须致电 std::cout 或致电 using std::cout;

以下是您的原始代码中的错误:

  1. 在主函数中,您没有使用对象调用该方法 getQuant()。您将需要使用对象调用 getQuant() 方法来修复该错误。

  2. 方法中的第一个错误Array::getQuant()

要比较 x 和 xp,您需要调用 getSphere(i).get_x() <= xp,因为您的 Array class 不包含数据成员 x,只有 Sphere class 包含数据成员 x。因此,您需要通过 class Sphere.

的对象访问数据成员 x
  1. 方法中的第二个错误Array::getQuant()

    您写了 "quant++"quant 定义在任何地方。我认为您打错了字,应该输入 "quantity++"

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

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

同样,上述 2 种方法已得到修复,并且 运行 正确,正如我所验证的那样。如果您 运行 遇到任何问题,请告诉我。