CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Sep 2008
    Posts
    14

    syntax error : missing ';' before '*'

    I am getting a syntax error : missing ';' before '*' in my code. I have written 6 different classes, and all of them link to at least one other class.
    Here are my class names:
    Vertex, VertList, Edge, EdgeList, AdjList, and Graph.


    I have checked every class about 5 times for a missing semicolon (yes I checked the ones at the end of the class declaration, too)

    So it is either being extremely elusive or its something else.
    Is this a problem:

    Vertex includes EdgeList
    EdgeList includes Edge
    Edge includes Vertex

    Thats the only thing I can think of, is that it is somehow freaking visual studio out.
    I would post my code, but I don't think you want to go through 6 classes and their definitions.

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    Re: syntax error : missing ';' before '*'

    Most likely, you forgot the semicolon after your class definition. I miss it every time :P

    Code:
    classdef{
       public:
          classdef(void);
    };

  3. #3
    Join Date
    Sep 2008
    Posts
    14

    Re: syntax error : missing ';' before '*'

    No, no, thats what I meant to say. I've checked those and every other line of code several times.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: syntax error : missing ';' before '*'

    You've probably got an include cycle. With proper include guards on the classes, an include cycle means
    A gets pasted inside C gets pasted inside B gets pasted inside A
    where "gets pasted inside" is the intuitive way to think of what a #include really does.
    Except, since the final A's include guard has already been defined, the innermost A actually has empty contents! Hence the error.

    The solution is to break the include cycle by, for at least one of the classes, removing the #include and relying solely on forward-declarations in the header file. The source file can still have the include, naturally.

    In general, headers should limit their includes in favor of forward-declares as much as possible, while source files should go nuts on the includes.

  5. #5
    Join Date
    Feb 2002
    Posts
    4,640

    Re: syntax error : missing ';' before '*'

    Quote Originally Posted by sneakyarab
    Vertex includes EdgeList
    EdgeList includes Edge
    Edge includes Vertex
    Quote Originally Posted by Lindley
    A gets pasted inside C gets pasted inside B gets pasted inside A


    Viggy

  6. #6
    Join Date
    Sep 2008
    Posts
    14

    Re: syntax error : missing ';' before '*'

    OK that makes sense but I'm not really sure how I can do this without the includes

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: syntax error : missing ';' before '*'

    Paste one of the headers here. Maybe we can suggest a way.

  8. #8
    Join Date
    Sep 2008
    Posts
    14

    Re: syntax error : missing ';' before '*'

    OK here are the headers that are in the cycle:

    #ifndef VERTEX_H
    #define VERTEX_H
    #include <iostream>
    #include "EdgeList.h"
    using namespace std;
    class Vertex{
    char label;
    Vertex* next;
    int inEdges, outEdges;
    EdgeList* inEdgeP;
    EdgeList* outEdgeP;
    public:
    Vertex();
    Vertex(char x, Vertex* n);
    char getLabel();
    void setLabel(char x);
    void setNext(Vertex* x);
    Vertex* getNext();
    void addIn();
    void addOut();
    void subIn();
    void subOut();
    int getIn();
    int getOut();
    void setInP(EdgeList* e);
    void setOutP(EdgeList* e);
    EdgeList* getInP();
    EdgeList* getOutP();
    };
    #endif


    --------------------------------------------------------------------------

    #ifndef EDGELIST_H
    #define EDGELIST_H
    #include "Edge.h"
    using namespace std;
    class EdgeList{
    Edge* start;
    Edge* next;
    int n;
    public:
    EdgeList();
    EdgeList(Edge* s, Edge* n);
    Edge* getNext();
    void setNext(Edge* e);
    int size();
    Edge* getStart();
    };
    #endif

    --------------------------------------------------------------------------

    #ifndef EDGE_H
    #define EDGE_H
    #include "Vertex.h"
    using namespace std;
    class Edge{
    Vertex* start;
    Vertex* finish;
    Edge* next;
    int label;
    public:
    Edge();
    Edge(Vertex* s, Vertex* f, int label, Edge* next);
    void setStart(Vertex* s);
    Vertex* getStart();
    void setFinish(Vertex* f);
    Vertex* getFinish();
    void setLabel(int x);
    int getLabel();
    Edge* getNext();
    void setNext(Edge* e);
    };
    #endif

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: syntax error : missing ';' before '*'

    Next time, code tags.

    Fortunately for you, your design lends itself to solving this easily. Simply replace the relevant
    Code:
    #include "Filename.h"
    with
    Code:
    class Filename;
    in all three files.

  10. #10
    Join Date
    Sep 2008
    Posts
    14

    Re: syntax error : missing ';' before '*'

    It didnt seem to like that either, I switched around using class and include between all the files and couldnt get it working right at all. I changed the Edge class to just have character values where the vertexes were, and have been changing code accordingly. Im down from 200+ errors to less than 10, fixable errors now.

  11. #11
    Join Date
    Apr 2004
    Posts
    102

    Re: syntax error : missing ';' before '*'

    I noticed that you have using namespace std; in your header files, that can cause problems as well. using namespace whatever; should only be in your implementation files.

    Similarly, you have #include <iostream> in your Vertex.h but do not make use of it.
    Last edited by jefranki; April 24th, 2009 at 05:57 PM. Reason: grammar

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured