小编Hal*_*ona的帖子

C++是否有指定一系列值的比较运算符?(比如E语言中的'in')?

我需要写一个条件来检查值范围内的枚举变量是否可以用E语言完成:

enum EnumVariable {a, b, d, g, f, t, k, i};
if (EnumVariable in [ a, g, t, i]) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

在C++中有没有比问4次更好的方法if EnumVariable==a or EnumVariable==b

c++ comparison-operators

26
推荐指数
6
解决办法
2247
查看次数

C:使用extern时对变量的未定义引用

我尝试声明一个全局变量config:

//general.h

struct config_t {
    int num;
};

extern struct config_t config;  //The global variable
Run Code Online (Sandbox Code Playgroud)

然后我在下面定义配置变量general.c:

//general.c
#include "general.h"

struct config_t config = {
    num = 5;
};
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试在我的main函数中使用全局变量'config'时,我得到错误:

undefined reference to `config':
Run Code Online (Sandbox Code Playgroud)

主程序:

//main.c
#include "general.h"

main() {
    config.num = 10;
}
Run Code Online (Sandbox Code Playgroud)

为什么?

c global-variables extern

8
推荐指数
1
解决办法
2万
查看次数

为什么文件读取后是空的?

我对 Python 很陌生。我想处理现有文件 ( exist_file),并创建它的副本。问题是,当我创建文件的副本时,它exist_file变成空的。

exist_file = open('some_pass/my_file.txt', 'r')
print exist_file.read() # Here the file is successfully printed
copy_of_file = open('new_copied_file.txt', 'w')
copy_of_file.write(exist_file.read())
print exist_file.read() # Here the file is empty
Run Code Online (Sandbox Code Playgroud)

为什么exist_file是空的?

python file

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

结构选项错误:数组类型具有不完整的元素类型

我尝试构建一个用于解析 cmd 行的函数。但是,当我定义long_options数组时,出现编译错误:

error: array type has incomplete element type
error: field name not in record or union initializer
error: (near initialization for 'long_options')
// and so on for every defined line in the 'long_options' 
Run Code Online (Sandbox Code Playgroud)

编码:

//parse_cmd.c
void parse_cmd(int argc, char *argv[]) {
    while (1) {
        int input_char;
        static struct option long_options[] = {
                {.name = "dev-name", .has_arg = 1, .val = 'd'},
                {.name = "tcp-port", .has_arg = 1, .val = 't'},
                {.name = "ib-port",  .has_arg = 1, …
Run Code Online (Sandbox Code Playgroud)

c arrays getopt-long

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

如何在c ++中使用结构列表

我想构建一个包含Headers列表的struct Packet.我使用的相关代码:

//Packet.h
#include <list>

using namespace std;

struct Header {
    Header();
    bool Valid;
    long unsigned DestAddr:48;
};

struct Packet_t {
    Packet_t();
    list<Header> Headers;
};
Run Code Online (Sandbox Code Playgroud)

现在我尝试构建Packet_t的构造函数,该构造函数将初始化Headers列表以仅包含一个头 - FirstHeader:

//Packet.cpp
#include "Packet.h"

Header::Header() {
    Valid = false;
    DestAddr = 0;
};

Packet_t::Packet_t(){
    ValidPacket = false;
    Header FirstHeader(); //here I try to initialize the first Header using its constructor
    Headers.push_front(FirstHeader);
};
Run Code Online (Sandbox Code Playgroud)

我得到的错误:

Packet.cpp:错误:没有匹配函数来调用'std :: list> :: push_front(Header(&)())'

真的很感激任何帮助

c++ struct list

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

Pyinstaller:AttributeError:模块“enum”没有属性“IntFlag”

我需要将python代码编译为exe。我找到了一些如何做到这一点的指南,其中要求我为此安装 PyInstaller:

pip install --upgrade pyinstaller
Run Code Online (Sandbox Code Playgroud)

但我收到下一个错误:

C:\Users\alonat>pip install pyinstaller
Collecting pyinstaller
  Using cached PyInstaller-3.6.tar.gz (3.5 MB)
  Installing build dependencies ... error
  ERROR: Command errored out with exit status 1:
   command: 'c:\users\alonat\appdata\local\programs\python\python37-32\python.exe' 'c:\users\alonat\appdata\local\programs\python\python37-32\lib\site-packages\pip' install --ignore-installed --no-user --prefix 'C:\Users\alonat\AppData\Local\Temp\pip-build-env-yo59g2oq\overlay' --no-warn-script-location --no-binary :none: --only-binary :none: -i https://pypi.org/simple -- 'setuptools>=40.8.0' wheel
       cwd: None
  Complete output (14 lines):
  Traceback (most recent call last):
    File "c:\users\alonat\appdata\local\programs\python\python37-32\lib\runpy.py", line 193, in _run_module_as_main
      "__main__", mod_spec)
    File "c:\users\alonat\appdata\local\programs\python\python37-32\lib\runpy.py", line 85, in _run_code
      exec(code, run_globals)
    File "c:\users\alonat\appdata\local\programs\python\python37-32\lib\site-packages\pip\__main__.py", line 23, in …
Run Code Online (Sandbox Code Playgroud)

python pip pyinstaller

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