小编jam*_*lin的帖子

如何调用指向typedef结构中定义的函数的指针

以下代码有什么问题?
下面的parseCounter1()和parseCounter1()是两个函数.
我把它们的指针放在一起,const OptionValueStruct以便
在option_values []的每个元素
都经过时可以相应地调用它们:

typedef struct OptionValueStruct{  
    char counter_name[OPTION_LINE_SIZE];  
    int* counter_func;  
} OptionValueStruct_t;  

const OptionValueStruct option_values[] = {    
    {"Counter1", (*parseCounter1)(char*, char**)},  
    {"Counter2", (*parseCounter2)(char*, char**)},  
   };  

const OptionValueStruct *option = NULL;

for(int i = 0; i< sizeof(option_values)/sizeof(OptionValueStruct_t); i++){
    option = option_values + i ;  
    result = option->counter_func(opt_name, opt_val);  
}  
Run Code Online (Sandbox Code Playgroud)

c function-pointers

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

如何将 List/Map 初始化为默认参数?

我知道如何在构造函数中初始化其他变量,例如 int 或 String,但我不知道如何为 List 和 Map 执行此操作。

class StackOverFlowQuestion{
StackOverFlowQuestion({this.test='', this.map=?, this.list=?});
String test;
Map map;
List list;
}
Run Code Online (Sandbox Code Playgroud)

我应该用什么代替问号?

谢谢。

null initialization dart flutter

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

C++析构函数示例

我的C++有点生疏但我已经制作了一个程序来反转链表,现在我正在尝试为它编写正确的析构函数,但我不知道究竟要销毁什么.这是我的类定义:

class LinkedList
{
    private:ListElement *start;
    public:LinkedList();
    public:void AddElement(int val);
    public:void PrintList();
    public:void InvertList();
};

class ListElement
{
    public:int value;
    public:ListElement * link;
    public:ListElement(int val);
    public:ListElement();
};


class Stack
{

private:ListElement ** stack;
private:int index;
public:Stack(int size);
public:void push(ListElement * le);
public:ListElement * pop();

};
Run Code Online (Sandbox Code Playgroud)

堆栈用于反转列表时.无论如何......我将如何为这些编写析构函数?我刚在想:

对于ListElement,使值为0,链接为0(NULL).

对于LinkedList,遍历元素并为所有元素调用ListElementDestructor.

我不是很确定这一点,因为我理解析构函数会自动调用成员对象的析构函数,所以在这种情况下只能为LinkedList写一个空的析构函数吗?我不知道......这就是我要问的原因

对于堆栈,我不知道......在反转列表之后,指针已经为0(NULL),因为它们都是poped.

我有点困惑.有人可以帮忙吗?先感谢您.

c++ destructor linked-list

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

如何在构造函数体中初始化不可为空的成员?

我已经以这种方式在 Dart 中创建了我的类,但是我Non-nullable instance field 'text' must be initialized. Try adding an initializer expression, or add a field initializer in this constructor, or mark it 'late'.想知道是否有一种方法可以以“Python”风格来创建这种类,在此先感谢您。

class Lexer {
  String _text;
  int _pos;
  String _current_char;

  Lexer(String text) {
    this._text = text;
    this._pos = -1;
    this._current_char = '';
    this.advance();
  }

  void advance() {
    this._pos++;
    this._current_char = this._pos < this._text.length ? this._text[this._pos] : '';
  }
}
Run Code Online (Sandbox Code Playgroud)

dart

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

为什么在颤振中在类之外声明枚举?

我正在查看关于单选按钮的 flutter 官方文档,并注意到枚举已在类之外声明。为什么我们不能在类内部声明它们,如果可以,在类内部和外部声明它们有什么区别?

这是示例代码:

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: const Center(
          child: MyStatefulWidget(),
        ),
      ),
    );
  }
}

enum SingingCharacter { lafayette, jefferson }


class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({Key? key}) : super(key: key);

  @override
  State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}


class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  SingingCharacter? _character = SingingCharacter.lafayette; …
Run Code Online (Sandbox Code Playgroud)

enums class dart flutter

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

为什么我的通用 StatefulWidget 类在运行时会出现 TypeError?

我有一个通用StatefulWidget类,它有一个Function回调的通用类。当我尝试调用该回调时,我得到一个运行时TypeError

\n
\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1 EXCEPTION CAUGHT BY WIDGETS LIBRARY \xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\nThe following _TypeError was thrown building MyStatefulWidget<int>(dirty, state:\nMyState<int>#cacb3):\ntype \'(int) => Widget\' is not a subtype of type \'(dynamic) => Widget\'\n\nThe relevant error-causing widget was:\n  MyStatefulWidget<int>\n  MyStatefulWidget:file:///path/to/my_flutter_project/lib/main.dart:11:13\n\nWhen the exception was thrown, this was the stack:\n#0      MyState.build (package:my_flutter_project/main.dart:33:19)\n
Run Code Online (Sandbox Code Playgroud)\n

可重现的例子:

\n
\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1 EXCEPTION CAUGHT BY WIDGETS LIBRARY \xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\nThe following _TypeError was thrown building MyStatefulWidget<int>(dirty, state:\nMyState<int>#cacb3):\ntype \'(int) => Widget\' is not a subtype of type \'(dynamic) => Widget\'\n\nThe relevant …
Run Code Online (Sandbox Code Playgroud)

generics flutter

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

添加指针的C行为

鉴于:

int a[N];
int *p; 
Run Code Online (Sandbox Code Playgroud)

为什么a-p工作还a+p没有错误:"二进制+的操作数无效".

c arrays pointers

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