我认为这比Django更像是一个蟒蛇问题.
但基本上我在模特A做:
from myproject.modelb.models import ModelB
Run Code Online (Sandbox Code Playgroud)
在模型B:
from myproject.modela.models import ModelA
Run Code Online (Sandbox Code Playgroud)
结果:
无法导入名称ModelA
我做了一些禁止的事吗?谢谢
我们有两个班级:
template<typename T, typename Size, typename Stack, typename Sparse>
class Matrix
Run Code Online (Sandbox Code Playgroud)
和
template<typename T, typename Size>
class Iterator
Run Code Online (Sandbox Code Playgroud)
Matrix 应该能够返回开始和结束迭代器,并且 Iterator 将保持对 Matrix 的引用以通过其接口访问元素。我们不希望 Iterator 依赖于 Matrix 的内部存储来防止耦合。我们如何解决这个循环依赖问题?
(内部Storage类与Matrix类具有相同的模板参数,与Matrix本身具有相同的访问过程)
我目前的情况如下:
struct A {
shared_ptr<B> b;
};
struct B {
shared_ptr<A> a;
};
//...
shared_ptr<A> a(new A());
shared_ptr<B> b(new B());
a->b(b);
b->a(a);
Run Code Online (Sandbox Code Playgroud)
我知道这不起作用,因为引用将继续指向彼此.我也被告知weak_ptr解决了这个问题.
但是,weak_ptr没有得到或->超载.我听说过"使用lock()",但任何人都可以提供如何正确执行此操作的代码示例吗?
通常,如果我的#include链变为循环,我通过用前向声明替换#includes中的一个来解决它,然后将依赖于此类型的所有函数实现移动到cpp文件中,其中我改为#include头.
但是 - 在某些情况下,将函数实现放入cpp文件是很糟糕的 - 特别是在处理模板或内联函数时.
因此 - 是否有其他方法来处理循环#include链而不是使用前向声明?
谢谢!
我将在这个问题上使用C++übernbb,并询问在继承时如何处理循环依赖关系是最好的方法.
设置很简单:Scene类扩展了Actor; 场景有一个指向Actors矢量的指针; Actor有(父)场景的指针.
至于我得到的包含文件:
Scene.h:
#include <string>
#include <vector>
using namespace std;
#ifndef __Scene_h__
#define __Scene_h__
#include "Actor.h"
namespace myns
{
// class Actor;
class Scene;
}
namespace myns
{
class Scene: public myns::Actor
{
/* private class attributes... */
public:
/* public class attributes... */
std::vector<myns::Actor*> actors;
Scene(/* arguments */);
/* public class methods... */
};
}
#endif
Run Code Online (Sandbox Code Playgroud)
Actor.h
#include <string>
#include <vector>
using namespace std;
#ifndef __Actor_h__
#define __Actor_h__
#include "Scene.h"
namespace myns
{
// class Scene; …Run Code Online (Sandbox Code Playgroud) 你好,我在使用前向声明时遇到了麻烦.我无法访问转发的类函数,但我需要这样做.
这是我的Window.h:
#include "Tab.h" // Needed because Window will create new Tabs
class Window {
public:
...
void doSome();
};
Run Code Online (Sandbox Code Playgroud)
这是Tab.h:
class Window; // forward delcaration
class Tab {
public:
class Tab(Window *parent);
void callParentFunction();
private:
Window *m_parent;
};
Run Code Online (Sandbox Code Playgroud)
最后,Tab.cpp:
#include "Tab.h"
Tab::Tab(Window *parent) {
m_parent = parent;
}
Tab::callParentFunction() {
m_parent->doSome(); // Error
}
Run Code Online (Sandbox Code Playgroud)
编译器返回以下错误:无效使用不完整类型'struct Window'
如何知道它已经包含Tab.h来创建标签,我如何访问父节点的功能?如果我不能,你建议我做什么?
所以我要解决这个导入周期。我有以下模式:
view/
- view.go
action/
- action.go
- register.go
Run Code Online (Sandbox Code Playgroud)
总体思路是,操作是在视图上执行的,并由视图执行:
// view.go
type View struct {
Name string
}
// action.go
func ChangeName(v *view.View) {
v.Name = "new name"
}
// register.go
const Register = map[string]func(v *view.View) {
"ChangeName": ChangeName,
}
Run Code Online (Sandbox Code Playgroud)
然后在view.go中调用它:
func (v *View) doThings() {
if action, exists := action.Register["ChangeName"]; exists {
action(v)
}
}
Run Code Online (Sandbox Code Playgroud)
但这会导致一个周期,因为View依赖于Action包,反之亦然。我该如何解决这个周期?有其他方法可以解决此问题吗?
我有 3 个查询参数经度、纬度和半径。
我有 3 个可能的条件:
在所有其他情况下发送验证错误。
例如
经度=3.12 - 错误
纬度=2.12,半径=3.2 - 误差
经度=12.12,纬度=2.12 - 好的
我的架构看起来像这样:
const schema = Joi.object().keys({
longitude: Joi.number().optional().error(new Error('LBL_BAD_LONGITUDE'))
.when('latitude', { is: Joi.exist(), then: Joi.number().required() })
.when('radius', { is: Joi.exist(), then: Joi.number().required() }),
latitude: Joi.number().optional().error(new Error('LBL_BAD_LATITUDE'))
.when('longitude', { is: Joi.exist(), then: Joi.number().required() })
.when('radius', { is: Joi.exist(), then: …Run Code Online (Sandbox Code Playgroud) 我需要向减法圆添加圆形径向渐变。我一直在尝试,但我无法获得圆形渐变。
1:整圆2:整圆径向渐变3:减圆4:减圆中的圆形径向渐变(不是我想要的)5:减圆中的圆形径向渐变。这就是我想要获得的。
一旦我得到减去的圆 (3),我就会应用径向渐变,但我得到的是 (4) 而不是 (5)。
int x = 0.5;
int y = 0.5;
RadialGradient gradientCut = new RadialGradient(0, 0, x, y, 1, true, CycleMethod.NO_CYCLE, new
Stop[] {
new Stop(0, Color.ORANGE),
new Stop(0.2, Color.YELLOW),
new Stop(0.5, Color.TRANSPARENT)
});
Rectangle rect = new Rectangle(0, 0, 1000, 75);
Shape cutCircleGradient = Shape.intersect(circleGradientCut, rect);
cutCircleGradient.setFill(gradientCut);
Run Code Online (Sandbox Code Playgroud)
我也尝试更改值 x 和 y 但我没有得到我想要的。
我在其中创建了一个以challenge.py以下代码命名的文件:
import challenge
def main():
print('Circular much...')
challenge.main()
Run Code Online (Sandbox Code Playgroud)
由此,我期望 python 由于导入正在运行的文件的循环导入而引发错误,但我发现在 python 3.7 和 3.8 上,该文件运行并打印了Circular much...两次。我会理解一次,因为这意味着文件的其余部分在导入时没有运行,我会理解递归错误,因为它在堆栈中无限地运行了challenge.main(),但我不明白为什么会这样打印两次然后停止?
c++ ×5
class ×2
python ×2
architecture ×1
boost ×1
dependencies ×1
django ×1
go ×1
gradient ×1
include ×1
intersect ×1
javafx ×1
joi ×1
node.js ×1
python-3.x ×1
recursion ×1
shapes ×1
templates ×1
validation ×1
weak ×1