首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C++循环文件结构

C++循环文件结构
EN

Stack Overflow用户
提问于 2022-11-16 18:32:43
回答 1查看 55关注 0票数 0

我有两个类Edge,在分隔的文件中的Node包含节点作为数据

代码语言:javascript
运行
复制
#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);
};

节点包含边缘作为数据

代码语言:javascript
运行
复制
#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
};

如何将Edge.h包含在Node.h中,Node.h包含在Edge.h中而不产生冲突?

我尝试在每个文件的第一行使用#杂注一次,但在进行循环时出现了一些错误

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
#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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-16 18:44:38

使用前直接声明“边缘”类,但不包含h文件.

代码语言:javascript
运行
复制
    #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
    };
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74465522

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档