I am with a project in which I work with several classes, one of these is the link class which stores different days with dynamic memory. On the other hand I have a Jornada case, this is where I have the following function:
int CJornada::PuntsTotalsJornada(CJornada& jornada)
{
int tmp;
for(int i=0;i<m_numPartits;i++)
{
tmp = jornada.m_partits[i].PuntsTotal();
m_puntsTotalsJornada=m_puntsTotalsJornada+tmp;
}
return m_puntsTotalsJornada;
}
I try to call it from the League class with the following function, but I can't get it to work:
void
CLliga::MostraJornadaAmbMesPunts(ostream& os)
{
//os<<"La Jornada amb mes punts es la i amb Puntostotales"<<endl;
for(int i=0;i<m_numJornades;i++)
{
CJornada::PuntsTotalsJornada(m_Jornades[i]);
}
}
It shows me the following error, but I can't think of how to fix it: cannot call member function int CJornada::PuntsTotalsJornada(CJornada&)
without object
This would be the League constructor:
CLliga::CLliga()
{
// Estableix el valor per defecte de les variables de CLliga
m_nomLliga = ("????");
m_temporada = ("0000-0000");
m_Jornades = 0;
m_numJornades = 0;
}
And its attributes:
private:
CCadena m_nomLliga;
CCadena m_temporada;
CJornada *m_Jornades;
int m_numJornades;
I add that the attribute M_Jornades
is a list of days that contains attributes such as the number of matches, I attach its constructor and attributes to clarify:
CJornada::CJornada()
{
// Defineix aqui el constructor per defecte
m_partits = 0;
m_numPartits = 0;
m_idJornada = 0;
m_puntsTotalsJornada=0;
}
private:
CPartit *m_partits;
int m_numPartits;
int m_idJornada;
int m_puntsTotalsJornada;
I guess what you want is to call a method without needing to apply it on an instance.
To do this, all you have to do is declare the method as
static
. Changing your classCJornada
:Applied to functions within classes, it allows just that: calling them without the need for an instance .
Note that, within methods
static
, you cannot accessthis
, since itthis
is precisely a pointer to the instance; We already said that the methodsstatic
do not have .static
is applied in the declaration ; the body of the function remains as it is, you cannot apply it.Note that functions
static
, since they do not havethis
, can not access any other class attributes that are not, in turn, static .CLiga
has as a memberm_Jornades
, which is a pointer of typeCJornada
. As you indicate, itm_Jornades
contains a list with all the available days.What you have to do is provide a concrete reference to one of those journeys so that the function knows which element to interact with. One possibility would be to pass the index to it:
Or the identifier of the day. In this case you have to iterate in
m_Jornades
to locate the element that has the identifier:Unless the function is static delara:
You should instantiate the object to be able to call that function: