我正在设计一个具有std::vector<int>实例变量的类.我正在使用a,std::vector因为我需要在运行时设置它的大小.以下是我的代码的相关部分:
my_class.h:
#include <vector>
using std::vector;
class MyClass {
    int size;
    vector<int> vec;
}
my_class.cc:
#include "my_class.h"
using std::vector
MyClass::MyClass(int m_size) : size(m_size) {
     vec = new vector<int>(size,0);
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译时,我收到以下错误消息:
g++ -c -Wall my_class.cc -o my_class.o
my_class.cc: In constructor ‘MyClass::MyClass(int):
  my_class.cc:4 error: no match for ‘operator=’ in ‘((MyClass*)this)->My_Class::vec = ((*(const allocator_type*)(& std::allocator<int>())), (operator new(24u), (<statement>, ((std::vector<int>*)<anonymous>))))’
make: *** [my_class.o] Error 1
Run Code Online (Sandbox Code Playgroud)
但是,当我将违规行更改为:
vector<int> temp(size,0);
vec = temp;
Run Code Online (Sandbox Code Playgroud)
它现在编译没有故障,我得到了所需的行为,并可以访问我的矢量
vec[i]  // i having been defined as an …Run Code Online (Sandbox Code Playgroud) 我有一些代码分布在三个文件中,我想使用第四个"gloabls"文件来存储一些物理常量,例如pi的值.这样可以避免重复定义pi = 4*atan(1.0).在探讨之后,我尝试过创建一个全局头文件:
/*globals.h*/
extern double g_pi;
Run Code Online (Sandbox Code Playgroud)
和一个全局cpp文件:
/*globals.cpp*/
#include "math.h"
#include "globals.h"
double g_pi = 4*atan(1.0);
Run Code Online (Sandbox Code Playgroud)
然后我将这些文件包含在我的主文件中:
/*mainFile.cpp*/
//Include math and other libraries 
#include globals.h"
int main() {
/*
....
*/
double x = 2*g_pi
/*
....
*/
}
Run Code Online (Sandbox Code Playgroud)
这给了我一个未定义的g_pi参考错误.我在Ubuntu上使用g ++编译器.希望它是一个简单的修复!非常感谢您的建议.如果需要更多细节,我很乐意为他们提供.
我有一个使用Python click包的命令行程序.我可以在本地安装和运行它,没问题:
pip install --editable . # (or leave out the editable of course)
Run Code Online (Sandbox Code Playgroud)
现在,我想创建一个可以独立分发和运行的可执行文件.通常情况下,因为我在Windows环境中的时候,我会用一个py2exe,pyinstaller或cx_Freeze.但是,这些包都不起作用.
更具体地说,它们都生成可执行文件,但可执行文件什么都不做.我怀疑这个问题是因为我的main.py脚本没有main功能.任何建议都会非常有用,提前谢谢!
可以使用从此处复制的代码重现问题.
hello.py
import click
@click.command()
def cli():
    click.echo("I AM WORKING")
Run Code Online (Sandbox Code Playgroud)
setup.py
from distutils.core import setup
import py2exe
setup(
name="hello",
version="0.1",
py_modules=['hello'],
install_requires=[
    'Click'
],
entry_points="""
[console_scripts]
hello=hello:cli
""",
console=['hello.py']
)
Run Code Online (Sandbox Code Playgroud)
如果有人可以提供工作的setup.py文件来创建可执行文件和任何其他所需的文件,那将非常感激.
从控制台:
python setup.py py2exe
# A bunch of info, no errors
cd dist
hello.exe
# no output, should …Run Code Online (Sandbox Code Playgroud) c++ ×2
constructor ×1
cx-freeze ×1
header ×1
new-operator ×1
py2exe ×1
pyinstaller ×1
python ×1
python-click ×1
stdvector ×1