Home > database >  C Cycle File Structure
C Cycle File Structure

Time:11-17

I have two class Edge, Node in separated files Edge contain Node as data

#pragma once
#include<iostream>
#include<vector>
#include"Node.h"

using namespace std;

class Edge
{
public:
    Node* parent;
    Node* child;
    Edge* prev;
    Edge(Node* _parent, Node* _child) : parent(_parent), child(_child) { }
    static Edge* create(Node* _parent, Node* child);
};

and Node contain Edge as data

#pragma once
#include<iostream>
#include<vector>
//#include"Edge.h"

using namespace std;

class Node
{
public:
    //vector<Edge> childern;
    virtual void print() = 0 { }
    //vector<Edge> extract(Edge* prev); // return children, and set prev to each element in children
};

How can I include Edge.h in Node.h and Node.h in Edge.h without making conflictions ?

I have tried to use #pragma once in the first line of each file but there are some errors appears when I make the cycle

Severity    Code    Description Project File    Line    Suppression State
Error   C2238   unexpected token(s) preceding ';'   AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    11  
Error   C2614   'Edge': illegal member initialization: 'child' is not a base or member  AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    13  
Error   C2614   'Edge': illegal member initialization: 'child' is not a base or member  AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    13  
Error   C2614   'Edge': illegal member initialization: 'child' is not a base or member  AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    13  
Error   C2614   'Edge': illegal member initialization: 'parent' is not a base or member AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    13  
Error   C2614   'Edge': illegal member initialization: 'parent' is not a base or member AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    13  
Error   C2614   'Edge': illegal member initialization: 'parent' is not a base or member AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    13  
Error   C2660   'Edge::create': function does not take 2 arguments  AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Source.cpp    14  
Error   C2039   'parent': is not a member of 'Edge' AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Source.cpp    15  
Error   C2065   '_child': undeclared identifier AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    13  
Error   C2065   '_child': undeclared identifier AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    13  
Error   C2065   '_child': undeclared identifier AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    13  
Error   C2065   '_parent': undeclared identifier    AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    13  
Error   C2065   '_parent': undeclared identifier    AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    13  
Error   C2065   '_parent': undeclared identifier    AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    13  
Error   C4430   missing type specifier - int assumed. Note: C   does not support default-int    AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    11  
Error   C4430   missing type specifier - int assumed. Note: C   does not support default-int    AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    12  
Error   C4430   missing type specifier - int assumed. Note: C   does not support default-int    AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    11  
Error   C4430   missing type specifier - int assumed. Note: C   does not support default-int    AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    12  
Error   C4430   missing type specifier - int assumed. Note: C   does not support default-int    AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    11  
Error   C4430   missing type specifier - int assumed. Note: C   does not support default-int    AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    12  
Error   C2061   syntax error: identifier 'Node' AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    13  
Error   C2061   syntax error: identifier 'Node' AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    14  
Error   C2061   syntax error: identifier 'Node' AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    13  
Error   C2061   syntax error: identifier 'Node' AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    14  
Error   C2061   syntax error: identifier 'Node' AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    13  
Error   C2061   syntax error: identifier 'Node' AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    14  
Error   C2143   syntax error: missing ';' before '*'    AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    11  
Error   C2143   syntax error: missing ';' before '*'    AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    12  
Error   C2143   syntax error: missing ';' before '*'    AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    11  
Error   C2143   syntax error: missing ';' before '*'    AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    12  
Error   C2143   syntax error: missing ';' before '*'    AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    11  
Error   C2143   syntax error: missing ';' before '*'    AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    12  
Error   C2238   unexpected token(s) preceding ';'   AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    11  
Error   C2238   unexpected token(s) preceding ';'   AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    12  
Error   C2238   unexpected token(s) preceding ';'   AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    12  
Error   C2238   unexpected token(s) preceding ';'   AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    11  
Error   C2238   unexpected token(s) preceding ';'   AI-Project  C:\Users\hazem\source\repos\AI-Project\AI-Project\Edge.h    12  

source.cpp

#include<iostream>
#include"Map.h"
#include"Edge.h"

using namespace std;

int main()
{
    Node* cairo = new Map("Cairo");
    Node* alex = new Map("Alex");
    Node* gize = new Map("Giza");
    Node* aswan = new Map("Aswan");

    Edge* edge = Edge::create(cairo, alex);
    edge->parent->print();
    
    return 0;
}

CodePudding user response:

Direct declaration of the class Edge before using, but without including the h-file.

    #pragma once
    #include<iostream>
    #include<vector>
    //#include"Edge.h"
    
    using namespace std;
    
    class Edge;
    class Node
    {
    public:
        vector<Edge> childern;
        virtual void print() = 0 { }        
        vector<Edge> extract(Edge* prev); // return children, and set prev to each element in children
    };
  •  Tags:  
  • c
  • Related