使用 class 方法而不实例化
Use a class method without instantiating
我的 POO 练习有问题。问题说:
Add to the Point class of the previous exercise two public inline
methods that indicate the number of instances of the object type that
have been created and how many are in memory. Note: Accountants must
be private members of the class. Justify invoking the getCantCreated
and getCantExisting methods before creating any object.
但是我编译代码,出现错误。它是:
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
How can I resolve it? I know the problem comes from not instantiating an object, but how do I use this statement Punto::getCantCreada()
如果你使 contInst
静态,你可以使 getCantCreada()
静态,然后你可以调用 Punto::getCantCreada()
:
class Punto
{
private:
...
static int contInst;
...
public:
...
static int getCantCreada();
...
};
int Punto::contInst = 0;
我的 POO 练习有问题。问题说:
Add to the Point class of the previous exercise two public inline methods that indicate the number of instances of the object type that have been created and how many are in memory. Note: Accountants must be private members of the class. Justify invoking the getCantCreated and getCantExisting methods before creating any object.
但是我编译代码,出现错误。它是:
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
How can I resolve it? I know the problem comes from not instantiating an object, but how do I use this statement
Punto::getCantCreada()
如果你使 contInst
静态,你可以使 getCantCreada()
静态,然后你可以调用 Punto::getCantCreada()
:
class Punto
{
private:
...
static int contInst;
...
public:
...
static int getCantCreada();
...
};
int Punto::contInst = 0;