print a list of struct

Im new and need you help ! I created a struct and would like to print it however I have difficulties.. First print function printstruct print each member of my struct and i would like the second to print each struct i add in the list…

#include <iostream>
#include <list>
#include <vector>
#include <stdint.h>


typedef struct ChanAccessReq {
    uint8_t code=1; //Code message7
    uint8_t bourrage=1; //Octet de bourrage
    char transMode='C';
    uint8_t linkAddr;

  }RTS;

void printstruct (ChanAccessReq demande)
{


  std::cout <<"Code du message : "<< unsigned(demande.code)<<'n';
  std::cout << "Taille de uint8_t : "<< sizeof(demande.code)<<'n';
  std::cout << "Bourrage message : "<< unsigned(demande.bourrage)<<'n';

  std::cout <<"Mode message : "<<demande.transMode<<'n';
  std::cout <<"caractère taille : "<<sizeof(demande.transMode)<<'n';
  std::cout <<"Adresse de lien : "<< unsigned(demande.linkAddr)<<'n';
};

void print(std::list<RTS> list)
{
        for (auto const& i: list) {
        printstruct(list);
    }

}

int main ()
{
  RTS test ;
  std::list<RTS> listbizar;
  listbizar.push_back(test);
  
  print(test);

  return 0;
};

When I run i get this error :

error: could not convert ‘list’ from ‘std::__cxx11::list’ to ‘ChanAccessReq’|

Answer

You have passed the wrong arguments, with my fix the code works.

printstruct(i); and print(listbizar); should be right.

Note that I also changed the definition of void printstruct(const ChanAccessReq& demande) with reference argument to avoid unnecessary copy.

I also suggest you using vector instead of list for containers with small numbers of elements. Which is more cache-friendly. And list is used when you want to insert and delete the elements in the non tail part of the containers, which is not the same with you user case.

#include <stdint.h>

#include <iostream>
#include <list>
#include <vector>

typedef struct ChanAccessReq {
  uint8_t code = 1;      // Code message7
  uint8_t bourrage = 1;  // Octet de bourrage
  char transMode = 'C';
  uint8_t linkAddr;

} RTS;

void printstruct(const ChanAccessReq& demande) {
  std::cout << "Code du message : " << unsigned(demande.code) << 'n';
  std::cout << "Taille de uint8_t : " << sizeof(demande.code) << 'n';
  std::cout << "Bourrage message : " << unsigned(demande.bourrage) << 'n';

  std::cout << "Mode message : " << demande.transMode << 'n';
  std::cout << "caractère taille : " << sizeof(demande.transMode) << 'n';
  std::cout << "Adresse de lien : " << unsigned(demande.linkAddr) << 'n';
};

void print(std::list<RTS> list) {
  for (auto const& i : list) {
    printstruct(i);
  }
}

int main() {
  RTS test;
  std::list<RTS> listbizar;
  listbizar.push_back(test);

  print(listbizar);

  return 0;
};