2011-10-16 7 views
-1

Le programme de recherche C++ Depth-first suivant ne sera pas compilé.Implémentation de l'algorithme de recherche en profondeur en profondeur

#include <iostream> 
using namespace std; 

class Stack 
{ 

private: 
     const int size=20; 
     int *st; 
     int top; 
public : 
     Stack(){ 
    st =new int[size]; 
    top=-1; 

     } 
     ~Stack(){ 
      delete[] st; 
      top=-1; 
     } 
     void push(int j){ 
      st[++top]=j; 
       } 
     int pop(){ 
      return st[top--]; 
     } 
     int peek(){ 

      return st[top]; 

     } 
     bool empthy(){ 
      return (top==-1); 

     } 
}; 
class Vertex{ 
public: 
    char label; 
    bool visited; 
public: 
    Vertex(){ 


    } 
    Vertex(char lab){ 
     label=lab; 
     visited=false; 

    } 
    }; 
class Graph{ 
private: 
     const int maxvertex=20; 
     Vertex* vertexlist; 
     int **adj; 
     int nverts; 
     Stack *stack; 
public: 
    Graph(){ 
    vertexlist=new Vertex[maxvertex]; 
    adj=new int*[maxvertex]; 
    for (int i=0;i<20;i++) 
      adj[i]=new int[maxvertex]; 
    nverts=0; 
     for (int i=0;i<maxvertex;i++){ 
      for (int j=0;j<maxvertex;j++){ 
       adj[i][j]=0; 
      } 
      } 

     stack=new Stack(); 
    } 
    void add(char lab){ 

     vertexlist[nverts++]=new Vertex(lab); 
    }1 




}; 
int main(){ 








    return 0; 
} 

Voici les erreurs de compilation que je reçois:

> 6 IntelliSense: no operator "=" matches these 
> operands c:\users\datuashvili\documents\visual studio 
> 2010\projects\dfs\dfs\dfs.cpp 76 23 DFS  7 IntelliSense: expected a 
> declaration c:\users\datuashvili\documents\visual studio 
> 2010\projects\dfs\dfs\dfs.cpp 77 3 DFS Error 1 error C2864: 
> 'Stack::size' : only static const integral data members can be 
> initialized within a class c:\users\datuashvili\documents\visual 
> studio 2010\projects\dfs\dfs\dfs.cpp 8 1 DFS Error 3 error C2864: 
> 'Graph::maxvertex' : only static const integral data members can be 
> initialized within a class c:\users\datuashvili\documents\visual 
> studio 2010\projects\dfs\dfs\dfs.cpp 54 1 DFS Error 2 error C2758: 
> 'Stack::size' : must be initialized in constructor base/member 
> initializer list c:\users\datuashvili\documents\visual studio 
> 2010\projects\dfs\dfs\dfs.cpp 12 1 DFS Error 4 error C2758: 
> 'Graph::maxvertex' : must be initialized in constructor base/member 
> initializer list c:\users\datuashvili\documents\visual studio 
> 2010\projects\dfs\dfs\dfs.cpp 60 1 DFS Error 5 error C2679: binary '=' 
> : no operator found which takes a right-hand operand of type 'Vertex 
> *' (or there is no acceptable conversion) c:\users\datuashvili\documents\visual studio 
> 2010\projects\dfs\dfs\dfs.cpp 76 1 DFS 
+2

Votre code semble manquer d'un certain nombre de lignes, et il ne se bloque évidemment pas car il ne compile pas. Surtout la première ligne avec une erreur est manquante. – thiton

Répondre

2

changement

const int size=20; 

à

static const int size=20; 

(statique signifie qu'il sera initialisé une fois par classe , pas par objet qui nécessiterait une liste d'initialisation)


vertexlist[nverts++]=new Vertex(lab); 

que vous essayez de définir un Vertex à un Vertex*. Cela ne compilera pas.

+0

Ce n'est pas comme ça que vous initialisez les consts ou les membres statiques en C++ (au moins avant C++ 11 pour lequel il n'y a pas de compilateur totalement conforme à partir du 2011/11/16) –

+1

@TamasSzelei Que voulez-vous dire? 'static const int size = 20;' est légal. – Pubby

+0

Vous avez raison, je pensais aux membres statiques (par opposition aux membres const constants). –

Questions connexes