Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

goddammit, C++

Name: Anonymous 2009-04-24 18:28

Hey, /prog/.  I have encountered a frustrating problem in my project.  I've stripped my code down to the bare essentials and I still don't know why this is fucking up.

main.cpp:


#include <iostream>
#include "vertex.h"
#include "edge.h"
using namespace std;

int main()
{
    Vertex v;
    Edge e;
    cout << "It works." << endl; //lol, no.
}


edge.h:


#ifndef Edge_h
#define Edge_h

#include "vertex.h"

class Edge
{
public:
    int label;
    Vertex v; // This line does not compile.  edge.h does not know what a vertex is.

    Edge() : label(1) {}
};

#endif


vertex.h:


#ifndef Vertex_h
#define Vertex_h

#include "edge.h"

class Vertex
{
public:
    int label;
    Edge e;  // This line compiles just fine.  vertex.h knows what an Edge is.

    Vertex() : label(1) {}
};

#endif


Errors:
Error    1    error C2146: syntax error : missing ';' before identifier 'v'
Error    2    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error    3    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   

So, can anyone tell me what ridiculously stupid thing I'm doing and/or ridiculously simple thing I'm missing?  This is driving me nuts.

Name: Anonymous 2009-04-24 18:43

You have a circular reference by value -- Edge HAS A vertex; Vertex HAS A Edge.  Fortunately Sepples is too brain-dead to realise the full horror of this recursion (infinite size structures).  You have to break the loop of knowing how big each instance is:

#ifndef Vertex_h
#define Vertex_h

class Edge; // forward reference

class Vertex
{
public:
    int label;
    Edge * e;  // This line compiles just fine.  vertex.h knows what an Edge is.

    Vertex() : label(1) {}
};

#endif


and

#ifndef Edge_h
#define Edge_h

class Vertex; // forward reference

class Edge
{
public:
    int label;
    Vertex * v;

    Edge() : label(1) {}
};

#endif

Name: Anonymous 2009-04-24 18:46

If every Vertex structure contains an Edge, and every Edge contains a Vertex, what is sizeof(Vertex)?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List