我有2个班级:DataObject和DataElement.DataObject保持指针(只)DataElementS,和一个DataElement包含指向几种类型,其中一个DataObject.
这曾经没有问题,因为我只使用指针DataObject输入DataElement,所以DataObject在标题中的前向声明DataElement就足够了.
但是,现在,我尝试添加一个析构函数DataElement,我需要删除一个DataObject.在这方面,编译器说:
dataelement/destructor.cc: In destructor ‘DataElement::~DataElement()’:
dataelement/destructor.cc:8: warning: possible problem detected in invocation of delete operator:
dataelement/destructor.cc:8: warning: invalid use of incomplete type ‘struct DataObject’
dataelement/dataelement.h:7: warning: forward declaration of ‘struct DataObject’
dataelement/destructor.cc:8: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined. …Run Code Online (Sandbox Code Playgroud) c++ destructor class-design circular-dependency forward-declaration
我遇到了如下所示的一些情况,其中每个类都需要另一个类,并且它创建了循环依赖。我在使用 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
我正在进行文本冒险,将关卡存储为一个名为"地方"的大型词典.我没有把它放在主文件中,而是认为我会创建一个名为'levels.py'的单独文件来包含它,使我的代码更清晰,并且无需经过450多行其他代码添加到它.
那么,主要的游戏文件:
from levels import places
class Thing:
#Some stuff
Run Code Online (Sandbox Code Playgroud)
levels.py:
from game import *
places = {
"bleh" : Thing("bleh"),
}
Run Code Online (Sandbox Code Playgroud)
然而,似乎"地方"并未在游戏中定义.
我认为正在发生的是有一个导入'循环'.但是,如果levels.py需要从game.py导入类,我怎么能阻止类似的东西呢?
最初的问题是
我有三个班级A,B并且C. A有一个链接并调用到B,它有一个链接并调用到C。但是C有一个对A静态方法的调用。我就是无法让这个静态调用起作用。
但是人们对我截断代码的一些不准确之处嗤之以鼻,因此类的真实名称是 GameRenderer,GameView和FieldView。GameRenderer有一个链接并调用到GameView,它有一个链接并调用到FieldView。但是FieldView有一个对GameRenderer静态方法的调用createGlTextureFromResource。我就是无法让这个静态调用起作用。
这是完整和真实的标题 GameRenderer.h
// GameRenderer.h
#ifndef GAME_RENDERER
#define GAME_RENDERER
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
namespace game{
class GameModel;
class GameView;
class GameRenderer {
jobject* context;
jobject* gameOverHandler;
static JNIEnv* env;
static jobject* jparent;
GameModel* gameModel;
GameView* gameView;
glm::mat4 mProjMatrix;
glm::mat4 mVMatrix; …Run Code Online (Sandbox Code Playgroud) 我有以下课程
class a {
std::shared_ptr<b> b_ref;
public:
a(std::shared_ptr<b> b_ref) : b_ref(b_ref) {}
};
class b {
std::shared_ptr<a> a_ref;
public:
b(std::shared_ptr<a> a_ref) : a_ref(a_ref) {}
};
Run Code Online (Sandbox Code Playgroud)
现在我想创建两个相互引用的对象有没有办法做到这一点?我认为使用c风格的指针我可以做到这一点.它也适用于共享指针吗?我尝试了reset()和swap()方法没有任何成功.
std::shared_ptr<a> a_ref;
std::shared_ptr<b> b_ref;
a_ref = std::make_shared<a>(b_ref);
b_ref = std::make_shared<b>(a_ref);
Run Code Online (Sandbox Code Playgroud)
不工作.
尽管可视化工作室预编译器或其所谓的任何内容都将Graph识别为来自不同标头的类,但在构建之后我得到了最荒谬的错误,就好像我之前从未提及其他标题一样.首先我没有转发声明这两个类,下面的第一组错误来自于此,但后来我尝试了声明,并且类似的错误与类本身的结构有关.使用来自另一个类的函数生成它们,这些函数向我显示头文件没有任何内容.他们不了解彼此的功能,我不知道为什么.
Vertex.h:
#pragma once
#include "Graph.h"
#include <vector>
class Graph;
class Vertex
{
int unique_id;
int longestChain = 0;
int chainComponent_id;
std::vector<int> edges;
Graph* master;
public:
int get_id()
{
return unique_id;
}
int getChainComponent_id()
{
return chainComponent_id;
}
void setChainComponent_id(int id)
{
chainComponent_id = id;
}
int DFS(int, int);
Vertex(int id, std::vector<int> _edges, Graph* _master)
{
unique_id = id;
edges = _edges;
master = _master;
longestChain = 0;
chainComponent_id = -1;
}
};
Run Code Online (Sandbox Code Playgroud)
Graph.h:
#pragma once
#include "Vertex.h" …Run Code Online (Sandbox Code Playgroud) 如何分析哪个源文件导致“不允许导入循环”问题?错误消息不够清楚,无法让我解决问题:
package command-line-arguments
imports app.exap/i8/internal
imports app.exap/i8/internal/data/retrieves
imports app.exap/i8/internal/integration/datastore
imports app.exap/i8/internal/objects/modules
imports app.exap/i8/internal/data
imports app.exap/i8/internal/integration/datastore: import cycle not allowed
package command-line-arguments
imports app.exap/i8/internal
imports app.exap/i8/internal/data/retrieves
imports app.exap/i8/internal/integration/datastore
imports app.exap/i8/internal/objects/modules
imports app.exap/i8/internal/data
imports app.exap/i8/internal/objects/modules: import cycle not allowed
Run Code Online (Sandbox Code Playgroud) 我不敢问为什么我需要在模板声明.cpp结束.h时添加.因为它已在StackOverflow中多次回答.
但我的问题是,当我在标题末尾添加.cpp时,为什么它不是循环依赖,或者编译器如何不继续添加.cpp进入.h和.h进入.cpp?
C++ 11是否试图解决这个奇怪的模板要求?
@Edit:只包含头文件
#ifndef MYMAP
#define MYMAP
#include <iostream>
#include <string>
using namespace std;
template<typename ValType>
class MyMap
{
public:
MyMap();
~MyMap();
void add(string key, ValType val);
ValType getValue(string key);
private:
static const int NumBuckets = 99;
struct cellT
{
string key;
ValType val;
cellT* next;
};
cellT *buckets[NumBuckets];
int hash(string key, int numBuckets);
cellT* findCellForKey(string key, cellT *head);
MyMap(MyMap&);
MyMap operator = (MyMap&);
};
#include …Run Code Online (Sandbox Code Playgroud) c++ ×5
python ×2
c++11 ×1
class-design ×1
destructor ×1
go ×1
header ×1
header-files ×1
import ×1
linker ×1
module ×1
pointers ×1
templates ×1