标签: cyclic-reference

C++循环包含问题

我有这个文件logger.hpp:

#ifndef _LOGGER_HPP_
#define _LOGGER_HPP_

#include "event.hpp"

// Class definitions
class Logger {
public:
    /*!
     * Constructor
     */
    Logger();
    /*!
     * Destructor
     */
    ~Logger();
    /*!
     * My operator
     */
    Logger& operator<<(const Event& e);
private:
    ...
};

#endif
Run Code Online (Sandbox Code Playgroud)

而这个文件是event.hpp

#ifndef _EVENT_HPP_
#define _EVENT_HPP_

#include <string>

#include "logger.hpp"

// Class definitions
class Event {
public:
  /*!
   * Constructor
   */
  Event();
  /*!
   * Destructor
   */
  ~Event();

  /* Friendship */
  friend Logger& Logger::operator<<(const Event& e);
};

#endif
Run Code Online (Sandbox Code Playgroud)

好.在logger.hpp中我包含event.hpp,在event.hpp中我包含logger.hpp.

  • 我需要包含event.hpp,因为在logger.hpp中我需要定义运算符.

  • 我需要包含logger.hpp,因为在event.hpp中,要在类Event中定义友谊.

当然,这是一个循环递归.

我试过这个: …

c++ include-guards include header-files cyclic-reference

3
推荐指数
1
解决办法
6160
查看次数

如何使用 std::shared_ptr 检测或避免循环引用?

我知道有weak_ptr打破循环,但这是一个修复,在发现问题后。是否有可用于检测或避免循环引用的模式或工具?

c++ design-patterns smart-pointers cyclic-reference

3
推荐指数
1
解决办法
1564
查看次数

有没有办法用编码/ gob序列化循环数据结构?

我正在努力将神经网络库移植到Go.我希望能够保存和恢复训练有素的网络,所以我试图直接序列化它.问题是,网络结构在其字段中包含循环(神经元A与神经元B有连接,神经元B与神经元A有连接).每当我尝试使用encoding/gob序列化整个网络时,它就会因堆栈溢出而失败.

这是一个非常简单的代码示例,它以相同的方式中断:

package main

import (
    "bytes"
    "encoding/gob"
    "fmt"
    "log"
)

type P struct {
    Name    string
    Q *Q
}

type Q struct {
    Name string
    P *P
}

func main() {
    var network bytes.Buffer        // Stand-in for a network connection
    enc := gob.NewEncoder(&network) // Will write to network.
    dec := gob.NewDecoder(&network) // Will read from network.

    p := &P{ "P", nil }
    q := &Q{ "Q", p }
    p.Q = q

    err := enc.Encode(p)
    if err != nil { …
Run Code Online (Sandbox Code Playgroud)

go cyclic-reference gob

3
推荐指数
1
解决办法
708
查看次数

使用python deepcopy时的AttributeError

我有一个具有一类__eq____hash__重写,使其对象作为字典键.每个对象还带有一个字典,由同一类的其他对象键入.AttributeError当我尝试deepcopy整个结构时,我感到很奇怪.我在OsX上使用Python 3.6.0.

Python文档看起来好像deepcopy使用memo字典来缓存它已经复制的对象,所以嵌套结构应该不是问题.那我做错了什么?我应该编写自己的__deepcopy__方法来解决这个问题吗?怎么样?

from copy import deepcopy


class Node:

    def __init__(self, p_id):
        self.id = p_id
        self.edge_dict = {}
        self.degree = 0

    def __eq__(self, other):
        return self.id == other.id

    def __hash__(self):
        return hash(self.id)

    def add_edge(self, p_node, p_data):
        if p_node not in self.edge_dict:
            self.edge_dict[p_node] = p_data
            self.degree += 1
            return True
        else:
            return False

if __name__ == '__main__':
    node1 = Node(1)
    node2 = Node(2)
    node1.add_edge(node2, "1->2") …
Run Code Online (Sandbox Code Playgroud)

python deep-copy cyclic-reference

3
推荐指数
1
解决办法
914
查看次数

如何编译和运行相互依赖的插件jar

我必须将两个eclipse-plugin项目构建到两个独立的jar中,每个jar都依赖于另一个来进行编译.Eclipse IDE抱怨"周期依赖......".我如何构建这些插件罐?我想通过将它们放在eclipse/plugin文件夹中来运行这些插件应该是顺畅的.

java eclipse-plugin jar build cyclic-reference

2
推荐指数
1
解决办法
855
查看次数

循环依赖 - 总是错的?

1.我想知道以下结构是否不正确,原因是什么,以及解决方案是什么:假设我已经实现了网络游戏的客户端客户端有2个主要包:
A.GUI - 持有所有摆动Jpanels等
B.LogicEngine

在Logic引擎中,我有一个名为clientThread的类,其主要目标是与服务器通信以获取在Gui Panel上执行的命令,并且还可以通过Gui Panels上的用户选择发回信息.

2.为了做到这一点,我倾向于在clientThread中引用我的主Gui面板,反之亦然,在两类不同的项目之间进行循环引用是错误的吗?

3.在面向对象编程问题上执行要在类内部显示的内容如客户端线程是错误的,客户端线程负责管理游戏流程,尽管它在逻辑引擎包上?

4.如果Gui部分知道并使用逻辑部分是一个问题吗?

想听听一些建议
非常感谢

java sockets oop swing cyclic-reference

2
推荐指数
2
解决办法
223
查看次数

循环引用特征的Scala类型错误

我无法使用此代码.我想打一个特点,允许继承它有"孩子"一类,但显然,ChildsetParent方法想要一个P,但是却能得到Parent[P, C]代替.

package net.fluffy8x.thsch.entity

import scala.collection.mutable.Set

trait Parent[P, C <: Child[C, P]] {
  protected val children: Set[C]
  def register(c: C) = {
    children += c
    c.setParent(this) // this doesn't compile
  }
}

trait Child[C, P <: Parent[P, C]] {
  protected var parent: P
  def setParent(p: P) = parent = p
}
Run Code Online (Sandbox Code Playgroud)

types scala compiler-errors cyclic-reference

2
推荐指数
1
解决办法
45
查看次数

杰克逊在第三方课程中无限递归的解决方法

有没有办法以编程方式告诉杰克逊忽略财产?例如,按名称.

我的问题是我正在序列化第三方对象,其中一些具有导致的父/子循环依赖

com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError)
Run Code Online (Sandbox Code Playgroud)

因为我无法修改代码,所以打破循环依赖关系的常用方法对我来说不起作用.

我正在使用ObjectMapperObjectWriter:

ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.setVisibility(new VisibilityChecker.Std(JsonAutoDetect.Visibility.ANY));
writer = mapper.writerWithDefaultPrettyPrinter();
writer.writeValueAsString(object);
Run Code Online (Sandbox Code Playgroud)

而且我知道它们是高度可定制的,就像我在代码片段中包含的序列化包含和可见性一样,但我找不到它们实现类似内容的方式

mapper.ignoreProperty("parent");
Run Code Online (Sandbox Code Playgroud)

java json cyclic-reference jackson

2
推荐指数
1
解决办法
1946
查看次数

在MongoEngine中使用reverse_delete_rule时,如何使用循环或前向ReferenceField?

这段代码炸弹:

from mongoengine import *

class Employee(Document):
    name = StringField()
    boss = ReferenceField("Employee", reverse_delete_rule = NULLIFY)
Run Code Online (Sandbox Code Playgroud)

以下是例外情况:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "[…]/mongoengine/base.py", line 791, in __new__
    new_class = super_new(cls, name, bases, attrs)
  File "[…]/mongoengine/base.py", line 630, in __new__
    f.document_type.register_delete_rule(new_class,
    File "[…]/mongoengine/fields.py", line 757, in document_type
    self.document_type_obj = get_document(self.document_type_obj)
  File "[…]/mongoengine/base.py", line 136, in get_document
    """.strip() % name)
mongoengine.base.NotRegistered: `Employee` has not been registered
in the document registry.
Importing the document class automatically …
Run Code Online (Sandbox Code Playgroud)

forward-declaration cyclic-reference mongoengine

1
推荐指数
1
解决办法
1478
查看次数

内部类使用外部类的方法.这是Cyclic参考吗?怎么避免

我想知道在内部类中调用外部Class方法然后在外部类中使用内部Class方法被认为是不好的做法.

在这种情况下:在BidParser中,我调用属于外部类的方法updateMaps().另外,我在BidParser中调用了第二个内部类InputSanityChecker的方法.

这是不好的做法和反模式吗?我在这里创建一个上帝对象吗?(但是在其他外部类中可以使用更多函数)

编辑:我有两个变量Var1,Var2(让我们说)属于外部但是updateX和checkX方法需要.

public class Outer{


    public static void main( String[] args ){
        if(args.length == 1){
            File file = new File(args[0]);
            BidParser.parseBids(file);//<--- Question refers here
        }else{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            BidParser.parseBids(br);  //<--- and here
        }
    }


    private static void updateMaps(String[] elements){
        //.....blah blah
    }

    static class BidParser{
        public static void parseBids(File file){
            //.....blah blah
            InputSanityChecker.checkInput(elems);//<---second inner class method
            InputSanityChecker.checkMaps(elems);    //<---second inner class method
            updateMaps(elems);  //<-----outer class method

        } …
Run Code Online (Sandbox Code Playgroud)

java inner-classes cyclic-reference

1
推荐指数
1
解决办法
230
查看次数

如何解决 Python 中的这种循环依赖

我遇到了如下所示的一些情况,其中每个类都需要另一个类,并且它创建了循环依赖。我在使用 ctypes 包装一些 c 代码时遇到了这种类型的情况。已经有很多关于这个主题的帖子,但我觉得它们没有帮助,我需要一些例子。有关解决此问题的任何想法/示例都会有所帮助。

# Module A
from B import C2

class C1(object):
    def __init__(self):
        self.name = "C1"
        self.c2 = C2()


# Module B
from A import C1

class C2(object):
    def __init__(self):
        self.name = "C2"
        self.c1 = C1()

# Main
from A import C1

if __name__ == "__main__":
    o = C1()
    print o.name
Run Code Online (Sandbox Code Playgroud)

python circular-dependency circular-reference cyclic-reference

0
推荐指数
1
解决办法
5551
查看次数

避免由C++引起的循环引用包括头文件

我有3个类:A,B和C.C #include由B编辑,B #includ由A编辑.在C类中,我为按钮定义了一个处理程序,当按下按钮时,C将PostMessage对象A.我在C中包含A,我将有一个循环引用,那么我该怎么做才能避免这种循环引用?

编辑:所有包含都是在实现文件中.

c++ include cyclic-reference

0
推荐指数
1
解决办法
701
查看次数

尝试添加引用时c#中的循环依赖问题

我有两个类库(让我们说LibA和LibB).LibA正在使用LibB的一些功能,所以我在LibA中添加了LibB的参考.

现在LibB还需要使用一些LibA代码.由于循环依赖性,我现在无法将LibA的参考添加到LibB.我现在可以做什么,我的LibB可以访问LibA的代码?

c# cyclic-reference

0
推荐指数
1
解决办法
72
查看次数