我在行使POO时遇到问题。问题说:


  将两个公共内联添加到上一个练习的Point类中
  指示对象类型实例数量的方法
  已创建,并且内存中有多少。注意:会计师必须
  成为班上的私人成员。证明调用getCantCreated是合理的
  和getCantExisting方法,然后再创建任何对象。


但是我编译代码,出现错误。它的:

error: cannot call member function 'int Punto::getCantCreada()' without object

我该如何解决?

我知道问题来自未实例化对象,但是如何在不创建对象的情况下使用此语句Punto::getCantCreada()
我读到这可以用静态变量解决,但是在这种情况下:它的实现是什么样的?

代码是:

#include <iostream>
using namespace std;

class Punto{
  private:
    double mx;
    double my;
    int contInst = 0;
    int contExist = 0;
    void verificador1000();

  public:
    Punto(double x=0,double y=0);
    Punto(const Punto& p);
    ~Punto();
    int getCantCreada();
    int getCantExistente();
};

Punto::Punto(double x,double y){
  mx = x;
  my = y;
  contInst++;
  contExist++;
  verificador1000();
}
Punto::Punto(const Punto& p){
  mx = p.mx;
  my = p.my;
  contInst++;
  contExist++;
  verificador1000();
}

void Punto::verificador1000(){
  if(mx>1000) mx = 1000;
  if(mx<-1000) mx = -1000;
  if(my>1000) my = 1000;
  if(my<-1000) my = -1000;
}

int Punto::getCantCreada(){
  return contInst;
}
int Punto::getCantExistente(){
  return contExist;
}

void ff (void){
  Punto p,q,w;
  Punto h(34);
  Punto r=h;
  cout <<"a. Puntos Creados:"<<Punto::getCantCreada()<< " - Existentes:"<< r.getCantExistente()<<endl;
}

int main(int argc, char *argv[]){
  cout <<"1. Puntos Creados:"<<Punto::getCantCreada()<< " - Existentes:"<< Punto::getCantExistente()<<endl;
  Punto p(12.34,-56.78);
  cout <<"2. Puntos Creados:"<<p.getCantCreada()<< " - Existentes:"<< p.getCantExistente()<<endl;
  Punto h(p);
  cout <<"3. Puntos Creados:"<<Punto::getCantCreada()<< " - Existentes:"<< Punto::getCantExistente()<<endl;
  ff();
  cout <<"4. Puntos Creados:"<<Punto::getCantCreada()<< " - Existentes:"<< Punto::getCantExistente()<<endl;

  std::cin.get();
  return 0;
}




#include <iostream>
using namespace std;

class Punto
{
private:
    double mx;
    double my;
    int contInst = 0;
    int contExist = 0;
    void verificador1000();

public:
    Punto(double x=0,double y=0);
    Punto(const Punto& p);
    ~Punto();
    int getCantCreada();
    int getCantExistente();
};

Punto::Punto(double x,double y)
{
    mx = x;
    my = y;
    contInst++;
    contExist++;
    verificador1000();
}
Punto::Punto(const Punto& p)
{
    mx = p.mx;
    my = p.my;
    contInst++;
    contExist++;
    verificador1000();
}

void Punto::verificador1000()
{
    if(mx>1000)
        mx = 1000;
    if(mx<-1000)
        mx = -1000;
    if(my>1000)
        my = 1000;
    if(my<-1000)
        my = -1000;
}

int Punto::getCantCreada()
{
    return contInst;
}
int Punto::getCantExistente()
{
    return contExist;
}

void ff (void)
{
    Punto p,q,w;
    Punto h(34);
    Punto r=h;
    cout <<"a. Puntos Creados:"<<Punto::getCantCreada()<< " - Existentes:"<< r.getCantExistente()<<endl;
}

int main(int argc, char *argv[])
{
    cout <<"1. Puntos Creados:"<<Punto::getCantCreada()<< " - Existentes:"<< Punto::getCantExistente()<<endl;
    Punto p(12.34,-56.78);
    cout <<"2. Puntos Creados:"<<p.getCantCreada()<< " - Existentes:"<< p.getCantExistente()<<endl;
    Punto h(p);
    cout <<"3. Puntos Creados:"<<Punto::getCantCreada()<< " - Existentes:"<< Punto::getCantExistente()<<endl;
    ff();
    cout <<"4. Puntos Creados:"<<Punto::getCantCreada()<< " - Existentes:"<< Punto::getCantExistente()<<endl;

    std::cin.get();
    return 0;
}

最佳答案

error: cannot call member function 'int Punto::getCantCreada()' without object
  
  我该如何解决?我知道问题出在不实例化对象,但是如何使用此语句Punto::getCantCreada()


如果使contInst静态,则可以使getCantCreada()静态,然后可以调用Punto::getCantCreada()

class Punto
{
  private:
    ...
    static int contInst;
    ...
  public:
    ...
    static int getCantCreada();
    ...
};

int Punto::contInst = 0;

关于c++ - 使用类方法而不实例化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49834984/

10-09 13:35