C ++中的生成器 - 无效使用非静态数据成员 [英] Generators in C++ -- invalid use of nonstatic data member
问题描述
我理解这一点,至少是generator的函数(我在Python中使用它们)。我理解switch语句及其内容是如何形成的。但是,我得到这些错误。
I sort of understand this, at least the function of generators (I've used them in Python). I understand how the switch statement and its content is formed. However, I get these errors.
test.cpp: In constructor 'Foo::descent::descent(int)':
test.cpp:46: error: invalid use of nonstatic data member 'Foo::index_'
test.cpp: In member function 'bool Foo::descent::operator()(std::string&)':
test.cpp:50: error: invalid use of nonstatic data member 'Foo::bars_'
test.cpp:50: error: invalid use of nonstatic data member 'Foo::index_'
test.cpp:51: error: invalid use of nonstatic data member 'Foo::index_'
test.cpp:51: error: invalid use of nonstatic data member 'Foo::bars_'
test.cpp:52: error: invalid use of nonstatic data member 'Foo::index_'
。如果你有一个更好的方式来处理这一点,一定要分享,请。
Here's the code. If you have a better way of dealing with this, by all means share please.
#include <math.h>
#include <string>
#include <vector>
#include <iostream>
#ifndef __generator_h__
#define __generator_h__
// generator/continuation for C++
// author: Andrew Fedoniouk @ terrainformatica.com
// idea borrowed from: "coroutines in C" Simon Tatham,
// http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
struct _generator
{
int _line;
_generator():_line(0) {}
};
#define $generator(NAME) struct NAME : public _generator
#define $emit(T) bool operator()(T& _rv) { \
switch(_line) { case 0:;
#define $stop } _line = 0; return false; }
#define $yield(V) \
do {\
_line=__LINE__;\
_rv = (V); return true; case __LINE__:;\
} while (0)
#endif
class Foo {
int index_;
std::vector<std::string> bars_;
public:
Foo() {
index_ = 0;
bars_.push_back("Foobar");
bars_.push_back("Barfoo");
}
$generator(descent){
int j;
descent(int j) {
index_+=j;
}
$emit(std::string)
while(true) {
$yield(bars_[index_++]);
if(index_ >= bars_.size())
index_ = 0;
}
$stop;
};
//descent bar;
void InitGenerator() { index_ = 0; }
};
using namespace std;
int main()
{
//Foo::descent gen(1);
//for(int n; gen(n);) // "get next" generator invocation
// cout << n << endl;
return 0;
}
推荐答案
让我们来展开宏,看看它是怎样的:
Let's expand the macros to see how this really looks:
class Foo {
int index_;
std::vector<std::string> bars_;
public:
Foo() {
index_ = 0;
bars_.push_back("Foobar");
bars_.push_back("Barfoo");
}
struct descent: public _generator {
int j;
descent(int j) {
index_+=j;
}
bool operator()(std::string& _rv) {
switch(_line) { case 0:;
while(true) {
do {
_line=__LINE__;
_rv = (bars_[index_++]); return true; case __LINE__:;
} while (0);
if(index_ >= bars_.size())
index_ = 0;
}
} _line = 0; return false; }
};
//descent bar;
void InitGenerator() { index_ = 0; }
};
正如你所看到的,我们声明一个内部结构 Foo :: descent
。然而,与一些其他语言不同,C ++中的内部类不会自动具有指向其外部类的实例的指针。您需要添加到 descent
a Foo *
/ code>构造函数,并使用 Foo *
引用 index _
和 bars_
- 或将必要的成员移入下降
。
As you can see, we declare an inner structure Foo::descent
. However, unlike in some other languages, inner classes in C++ do not automatically have a pointer to an instance of their outer class. You need to either add to descent
a Foo *
which is passed in via the descent
constructor, and use that Foo *
to reference index_
and bars_
- or move the necessary members right into descent
.
真的不明白什么 Foo
这里是为所有...一切似乎都属于下降
。
To be honest, I don't really understand what Foo
here is for at all... Everything it does seems to belong right in descent
.
这篇关于C ++中的生成器 - 无效使用非静态数据成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!