Problem with function call

2

I'm with a project where I work with several classes. One of these is the league class which stores different days with dynamic memory. On the other hand I have a case Jornada, it is this 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 Liga class with the following function, but I can not 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 not think of how to fix it: can not call member function int CJornada::PuntsTotalsJornada(CJornada&) without object

This would be the league builder:

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, attached 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;
    
asked by ElPatrón 26.03.2017 в 22:30
source

3 answers

1

CLiga has as member m_Jornades , which is a pointer of type CJornada . As you indicate, m_Jornades contains a list with all available days.

What you have to do is provide a specific reference to one of those days so that the function knows with what element it has to interact. One possibility would be to pass the index:

void CLliga::MostraJornadaAmbMesPunts(ostream& os, size_t indiceJornada)
{
  for(int i=0;i<m_numJornades;i++)
  {
    m_Jornades[indiceJornada].PuntsTotalsJornada(m_Jornades[i]);
  }
}

Or the identifier of the day. In this case you have to iterate in m_Jornades to locate the element that has the identifier:

// Ignoro si ya dispones de una función similar a esta
CJornada* CLliga::BuscarJornada(int idJornada)
{
  CJornada* jornada = nullptr; // o 0 si no compilas con C++11

  for( int i=0; i<m_numJornades && nullptr == jornada; i++ )
  {
    if( m_Jornades[i].IdJornada() == idJornada )
      jornada = &m_Jornades[i];
  }

  return jornada;
}

void CLliga::MostraJornadaAmbMesPunts(ostream& os, int idJornada)
{
  CJornada* jornada = BuscarJornada(idJornada);

  if( nullptr != jornada )
  {
    for(int i=0;i<m_numJornades;i++)
    {
      jornada->PuntsTotalsJornada(m_Jornades[i]);
    }
  }
}
    
answered by 27.03.2017 / 09:00
source
1

I guess what you want is to call a method without needing to apply it on an instance.

To do this, you only have to declare the method as static . Changing your class CJornada :

class CJornada {
public:
  static int PuntsTotalsJornada( CJornada& jornada );
// ^^^ Añadido
  ...
};

Applied to functions within classes, it allows just that: call them without needing an instance .

Note that, within static methods, you can not access this , since this is precisely a pointer to the instance; we already said that the static methods do not have .

static applies in the statement ; the body of the function remains as it is, no can apply.

Note that the static functions, since they do not have this , can not have access to the other class attributes that are not, in turn, static strong>.

    
answered by 26.03.2017 в 23:16
0

Unless the function is static:

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

should instantiate the object in order to call that function:

void CLliga::MostraJornadaAmbMesPunts(ostream& os) {

    CJornada cJornada;

    //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]);
    }
}
    
answered by 26.03.2017 в 23:28