【问题标题】:A lot of errors in classes with constructors and function带有构造函数和函数的类中有很多错误
【发布时间】:2021-08-10 09:14:13
【问题描述】:

这是我的文件的代码

#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

你能帮我解决它们吗?

【问题讨论】:

  • 您正在调用方法,但不在对象的上下文中,例如 getQuant() 而不是 spheres.getQuant();我也会用其他东西重命名 Array 以避免名称冲突
  • 您的代码令人困惑。 1.xSphere 中是私有的,所以我推测您尝试使用sphere-&gt;get_x()

标签: c++ arrays class dynamic


【解决方案1】:

只是跟随每个错误:

./src/main.cpp:42:3: 错误:使用未声明的标识符“getQuant” getQuant(15,30);

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

你可能想要:

spheres->getQuant(15,30);

./src/main.cpp:97:14: 错误:“数组”中没有名为“x”的成员 if(this->x

getQuant 实现中,您有this-&gt;x,但Array 没有x 成员。


./src/main.cpp:99:9: 错误:使用未声明的标识符“quant” 量化++;

getQuant 实现中,您有quant++ 但未声明此变量。你的意思是quantity


./src/main.cpp:103:7: 错误:使用未声明的标识符“cout”;你是说'std::cout'吗?

cout&lt;iostream&gt; 的一部分,并带有std 名称规范。您必须致电std::cout 或致电using std::cout;

【讨论】:

    【解决方案2】:

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

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

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

    要将 x 与 xp 进行比较,您需要调用 getSphere(i).get_x() &lt;= xp,因为您的 Array 类不包含数据成员 x,只有 Sphere 类包含数据成员X。因此,您需要通过 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;
    }
    

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

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

    【讨论】:

      猜你喜欢
      • 2021-08-09
      • 2011-12-19
      • 1970-01-01
      • 2016-04-16
      • 1970-01-01
      • 1970-01-01
      • 2014-07-29
      • 2011-05-07
      • 1970-01-01
      相关资源
      最近更新 更多