I got a matrix adjency that i want to transform in an adjency list and i got this error that i cant figure out...


main.cpp|65|error: cannot convert 'int (*)[25]' to 'int**' for argument '1' to 'Ville** conversion(int**, Ville**, int)

main.cpp|70|error: cannot convert 'int (*)[25]' to 'int**' for argument '1' to 'void affichageMatrice(int**, int)

#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

struct Ville
{
    int v;
    Ville* suivante;
    Ville (int x, Ville* t)
    {
        v = x;
        suivante = t;
    }
};
typedef Ville *chemin;

/** Variable Globale */
// nombre de villes
int nbVilles;

int main()
{
     cout << "OK: " << endl;


    /** ---- ---- Fonction ---- ---- */
    void affichageMatrice (int ** , int );
    chemin *conversion (int ** , chemin * , int );
    void affichageListe (chemin * , int );
    void mapVoyageurCommerce (int mapVille[][25]);


    /** ---- ---- Variables ---- ---- */
    //fichier contenant les valeurs des distances entre les villes
    string nomFichier;  //nom
    ifstream Fichier;   //fichier

    //tableaux des chemins
    int matriceVilles [25][25];

    /** ---- ---- Programme ---- ---- */
    //Initialisation du nom de fichier
    nomFichier = "test.txt";

    //Ouverture du fichier
    Fichier.open(nomFichier.c_str());
    //Initialisation du nombre de villes
    Fichier>>nbVilles;

    //Remplissage de la matrice de chemin
    for(int i=0; i<nbVilles; i++)
    {
        for(int j=0; j<nbVilles; j++)
        {
             cout << "OK: " << endl;
            Fichier >> matriceVilles[i][j];
        }
    }

    /**conversion de la matrice en liste**/
    chemin *listeVilles = new chemin [nbVilles];
    listeVilles = conversion(matriceVilles, listeVilles,nbVilles); <------------ERROR
    cout << endl;

    /**Affichage de la matrice d'adjacence**/
    cout << "Matrice d'adjacence: " << endl;
    affichageMatrice (matriceVilles, nbVilles); <------------ERROR

    /**Affichage de la liste d'adjacence**/
    cout << "Liste d'adjacence: " << endl;
    affichageListe (listeVilles, nbVilles);

//Fermeture du fichier
Fichier.close();

//Chemin le plus court
//mapVoyageurCommerce (matriceVilles);

return 0;
}

/**Affichage de la matrice d'adjacence**/
void affichageMatrice (int **a, int nbVilles)
{
    for (int i = 0; i < nbVilles; i++)
        for (int j = 0; j < nbVilles; j++)
            if (a[i][j] == 1)
                cout <<  i << " "
                    << j << "." << endl;
} // fin de la fonction affichage


/**Conversion de la matrice en liste d'adjacence**/
chemin *conversion (int **b, chemin * liste, int nbVilles)
{

    // cree un tableau
    for (int i = 0; i < nbVilles; i++)
        liste[i] = 0;

    // cree la liste
    for (int i = 0; i < nbVilles; i++)
        for (int j = 0; j < nbVilles; j++)
        {
            liste[i] = new Ville(j, liste[i]);
        }
    return liste;
}


/**Pour afficher la liste**/
void affichageListe (chemin * liste, int nbVilles)
{
    for (int i = 0; i < nbVilles; i++)
    {
        for (chemin finger = liste[i]; finger != NULL; finger = finger->suivante)
        {
            cout << "" << i << " and "
                    << finger->v << "." << endl;
        }
    }
} // fin fonction affichage

void mapVoyageurCommerce (int mapVille[][25])
{

    //Fonction de calcul du cout d'un chemin
    int valeurChemin(int[],  int couts[][25]);

    //Initialisation du tableaux des villes
    int villes[nbVilles];
    for (int i = 0; i<nbVilles; i++)
    {
        villes[i]=i;
    }
    //Initialisation de la permutation courante
    int cheminMinimum[nbVilles];

    int minimum = -1;
    int chemin[nbVilles];
    int coutChemin = 0;

    do
    {

        coutChemin = valeurChemin(villes, mapVille);
        if(coutChemin<minimum || minimum==-1)
        {
            minimum = coutChemin;
            for (int k = 0; k<nbVilles; k++)
            {
                cheminMinimum[k] = villes[k];
            }
        }
    }
    while ( next_permutation ( villes, villes+nbVilles) );

    cout << "le chemin minimum est : " << endl;

    for (int l = 0; l<nbVilles; l++)
    {
        cout << cheminMinimum[l]  << ", ";
    }

    cout << " le cout est : " << minimum << endl;

    return;
}



int valeurChemin(int chemin[], int couts[][25])
{
    //valeur de retour (le cout du chemin)
    int retour = 0;

    for(int i=0; i<nbVilles-1; i++)
    {
        retour = retour + couts[chemin[i]][chemin[i+1]];
    }

    retour = retour + couts[chemin[nbVilles-1]][chemin[0]];

    return retour;
}

my code is in french so if you need any further info feel free to ask than you.

i have read ur post on passing a 2d array to an argument is not good but i dont know really how to fix this...

i have read ur post on passing a 2d array to an argument is not good but i dont know really how to fix this...

The above thread has some suggestions on how to solve this by using a different approach.
If you want to keep your approach, just cast your 2-dimensional array to int**:

listeVilles = conversion(reinterpret_cast<int**>(matriceVilles), listeVilles, nbVilles);

That should achieve what you want.

The simplest solution is to use std::vector<>

std::vector< std::vector<int> > matriceVilles( 25, std::vector<int>(25) ) ;

void affichageMatrice( const std::vector< std::vector<int> >& m ) ;

and so on.

In most cases, there is no good reason to mess around with C-style arrays of arrays in a C++ program.

The simplest solution is to use std::vector<>

std::vector< std::vector<int> > matriceVilles( 25, std::vector<int>(25) ) ;

void affichageMatrice( const std::vector< std::vector<int> >& m ) ;

and so on.

In most cases, there is no good reason to mess around with C-style arrays of arrays in a C++ program.

Thank you guys i tried both solutions and tehy both worked. But i'll go with vectors.

thank you very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.