我目前正在为语法树创建一个框架.我有一个文件夹/包syntax,其中包含一个语法树元素,它是每个文件中的一个类,结构看起来像:
syntax/List.py -> contains class List
syntax/Block.py -> contains class Block
syntax/Statement.py -> contains class Statement
Run Code Online (Sandbox Code Playgroud)
现在我想以一种我可以访问类的方式将文件夹导入到我的源文件中
block = syntax.Block()
Run Code Online (Sandbox Code Playgroud)
这有可能吗?到目前为止,我总是认为我需要syntax.Block.Block()的不是很好......
bind将函数对象创建bind为函数的参数时,我遇到了问题.
我有两个函数fa和fb.fa将函数作为参数.我将fa函数(fb在这种情况下)绑定到一个闭包中.我用两种不同的方式来做,如下所示.第一个工作,第二个不编译.
(我知道,我不需要绑定标记的行,因为没有参数.但当然这只是显示行为的最小例子,在实践中我将绑定参数fb)
void fa(function<void()> f){
f();
}
void fb(){
}
void test_which_compiles() {
auto bb = fb; // *** QUESTIONABLE LINE ***
auto aa = std::bind(fa, bb);
aa();
}
void test_which_fails() {
auto bb = std::bind(fb); // *** QUESTIONABLE LINE ***
auto aa = std::bind(fa, bb);
aa(); // COMPILE ERROR HERE
}
Run Code Online (Sandbox Code Playgroud)
调用时发生编译错误aa().
因此,当我将一个函数对象绑定为一个本身由bind创建的参数时,编译器似乎不接受.
编译错误是一长串我不认为与该行直接相关的东西.我只给出第一行:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/type_traits:3476:13:错误:没有匹配函数来调用'__invoke'__invoke(_VSTD :: declval <_Fp>(),_ VSTD :: declval …
假设,我有以下代码.B中有一个复制构造函数,它调用一个复制a的资源的方法.
现在我也有一个移动构造函数.在这种情况下,不应该复制a,而只是"窃取"现有资源中的资源.因此,我还实现了一个使用右值的init.但是当然,当我尝试用参数ba调用它时,这是一个左值...
有没有办法调用这种方法?
class A{
A(const A&& a){
// 'steal' resources from a
}
void init(A& a){
// init this A from another A by copying its resources
}
void init(A&& a){
// init this A from another A stealing its resources and tell the other a, it must not destroy resources upon destruction
}
};
class B{
A a;
B(B& b){
a.init(b.a)
}
B(B&& b){
a.init(b.a); // How to call init(A&& a)?
}
};
Run Code Online (Sandbox Code Playgroud) 以下代码是重现我的问题的最小代码.当我尝试编译,链接器无法找到operator==为Config:
Undefined symbols for architecture x86_64:
"operator==(Config<2> const&, Config<2> const&)", referenced from:
_main in test2.o
Run Code Online (Sandbox Code Playgroud)
该operator==是朋友Config.但是,当我不再声明operator==为朋友时,代码编译器没有错误.
template <int DIM>
class Config{
// comment the following line out and it works
friend bool operator==(const Config<DIM>& a, const Config<DIM>& b);
public:
int val;
};
template <int DIM>
bool operator==(const Config<DIM>& a, const Config<DIM>& b){
return a.val == b.val;
}
int main() {
Config<2> a;
Config<2> b;
a == b;
return 0;
} …Run Code Online (Sandbox Code Playgroud) 我不知道我是否有IDE /工具链问题,Mac问题或C++问题:
我想保存一个文件ofstream.这按预期工作.但是当我使用像"〜/ Desktop/test.txt"之类的波形符保存到文件路径时,ofstream::good()报告错误.但是,我可以成功使用像"/Users/Michael/Desktop/test.txt"这样的路径.
这适用于Xcode调试器,用于Eclipse的运行,以及从控制台直接调用exectuable.
这个代字号"〜"有什么问题?
我有一组值 a[i]。
然后我计算
for(...){
b[i] = log(a[i]);
}
Run Code Online (Sandbox Code Playgroud)
然后我总结
for(...){
s += c[i] * b[i];
}
Run Code Online (Sandbox Code Playgroud)
到目前为止这没有问题。
但对于某些人来说,i我a[i]可能为零并导致b[i] = log(0) = -Inf. 对于这些i,c[i]也为零 - 这些是一些无效的数据点。但zero*-Inf似乎给了NaN,我的总和搞砸了......
有没有办法c[i] * b[i]在c[i]is = 0时总是有= 0?
我看到的唯一方法是将所有零设置a[i]为一个小的非零值或检查零,但可能有更好的解决方案。
我使用 C++ 和std数学函数,但我正在寻找一种尽可能通用的方法。
我有一些特殊的数字类.该类可以用双构造:
struct FancyDouble{
FancyDouble& operator = (double v){
this->value = v;
return *this;
}
};
FancyDouble myFancyDouble = 5.0;
Run Code Online (Sandbox Code Playgroud)
类从提供了一种(隐含的)转换double到FancyDouble.有没有办法做相反的事情?所以当我FancyDouble在数字表达式中使用a 时,我想写:
double a = 123;
double b = b * myFancyDouble;
Run Code Online (Sandbox Code Playgroud)
我查看了参考文献,我认为这是不可能的,但我不是很确定.
当我在C/C++中使用这种语法时,正在阅读PETSc:
PetscInt i, n = 10, col[3], its;
PetscScalar neg_one = -1.0, one = 1.0, value[3];
Run Code Online (Sandbox Code Playgroud)
我不明白这里逗号的含义.它与元组有关吗?或者有什么东西超载?
我dict以一种简单的方式扩展为使用d.key符号而不是直接访问它的值d['key']:
class ddict(dict):
def __getattr__(self, item):
return self[item]
def __setattr__(self, key, value):
self[key] = value
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试腌制它时,它会调用__getattr__find __getstate__,这既不存在也不必要。使用以下方法解压时也会发生同样的情况__setstate__:
>>> import pickle
>>> class ddict(dict):
... def __getattr__(self, item):
... return self[item]
... def __setattr__(self, key, value):
... self[key] = value
...
>>> pickle.dumps(ddict())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in __getattr__
KeyError: '__getstate__'
Run Code Online (Sandbox Code Playgroud)
我必须如何修改类ddict才能正确选择?
我试图在Eigen中占据一个块:
Eigen::VectorXi v = Eigen::VectorXi::Zero(20);
v << 7, 10, 11, 14, 15, 16, 16, 1, 2, 3, 2, 3, 4, 5, 4, 5, 0, 0, 0, 0;
cout << "v = " << v << endl;
v = v.block(0, 0, 16, 1);
cout << "v = "<< v << endl;
Run Code Online (Sandbox Code Playgroud)
奇怪的是,v在获得该块后,前两个条目将为零.
该程序的输出如下:
v = 7 # start original vector from here
10
11
14
15
16
16
1
2
3
2
3
4
5
4
5
0
0 …Run Code Online (Sandbox Code Playgroud)