以下代码仅用于说明我的问题.
template<class T>
class array<T>
{
public:
// constructor
array(cap = 10):capacity(cap)
{element = new T [capacity]; size =0;}
// destructor
~array(){delete [] element;}
void erase(int i);
private:
T *element;
int capacity;
int size;
};
template<class T>
void class array<T>::erase(int i){
// copy
// destruct object
element[i].~T(); ////
// other codes
}
Run Code Online (Sandbox Code Playgroud)
如果我array<string> arr在main.cpp.当我使用时erase(5),对象element[5]被破坏但是空间element[5]不会被释放,我可以用来element[5] = "abc"在这里放一个新值吗?或者我是否应该使用placement new来在空间中添加新值element [5]?
当程序结束时,arr<string>将调用它自己的析构函数,它也会调用delete [] element.因此,字符串的析构函数将首先运行以销毁对象,然后释放空间.但是因为我已经明确地破坏了element[5],所以析构函数(由arr的destuctor调用)运行两次来破坏element[5] …
我有一个使用运算符重载的类,但有一些警告.
// base.h
class base {
public:
base();
base(int n);
virtual ~base();
virtual void printBase(std::ofstream & out);
virtual base & operator =(const base &);
friend std::ofstream & operator <<(std::ofstream & out, const base &);
private:
double * coeff;
int n;
};
// base.cpp
std::ofstream & operator<<(std::ofstream & fout, const base & obj)
{
for(int i =0; i<(obj.n)-1; i++)
{
fout << obj.coeff[i]<<"*x"<<i;
if(i<obj.n-2)
{
fout<<"+";
}
}
fout<<"="<<obj.coeff[(obj.n)-1];
return fout;
}
void base::printBase(std::ofstream & fout)
{
for(int i =0; i<n-1; …Run Code Online (Sandbox Code Playgroud) void arrayList<T>::erase(int theIndex) {
// valid index, shift elements with higher index
copy(element + theIndex + 1, element + listSize, element + theIndex);
element[--listSize].~T(); // invoke destructor
}
Run Code Online (Sandbox Code Playgroud)
代码 element[--listSize].~T()看起来很奇怪.它用于删除动态分配的元素.有谁知道为什么我们可以使用这种语法?
编辑
问题已经解决了.
Robentry [] Rb = new Robentry[robNum];
Run Code Online (Sandbox Code Playgroud)
我怎样才能Rb存入ArrayList<E> Rt,所以Rt.get(0) == Rb就是这样true.我不知道如何定义E的ArrayList<E>.
编辑:
如果我使用:
Robentry [] Rb = new Robentry[robNum];
List<Robentry []> Rt = new ArrayList<Robentry []>();
// initialize Rb
//...
//
Rt.add(Rb);
Run Code Online (Sandbox Code Playgroud)
如果我改变Rb[0],Rt.get(0)[0]也会改变.所以,我怎么能存储的内容Rb为Rt使Rt独立的Rb?

我使用限幅器库.在图中,红色和黑色是剪辑,绿色是多边形.代码如下所示.但是,我不明白为什么生成的联合多边形是(7 3 4 14 9 1 2 6).我认为它应该是(1 4 14 9)?数字是图中所示的顶点.
using System;
using ClipperLib;
using Polygon = System.Collections.Generic.List<ClipperLib.IntPoint>;
using Polygons = System.Collections.Generic.List<System.Collections.Generic.List<ClipperLib.IntPoint>>;
namespace ClipperLibrary_Test
{
class Program
{
static void Main(string[] args)
{
Polygons subj = new Polygons(1);
subj.Add(new Polygon(4));
subj[0].Add(new IntPoint(0, 0));
subj[0].Add(new IntPoint(0, 70));
subj[0].Add(new IntPoint(100, 70));
subj[0].Add(new IntPoint(100, 0));
Polygons clip = new Polygons();
clip.Add(new Polygon(4));
clip[0].Add(new IntPoint(40, 0));
clip[0].Add(new IntPoint(40, 100));
clip[0].Add(new IntPoint(150, 100));
clip[0].Add(new IntPoint(150, 0));
clip.Add(new Polygon(4)); …Run Code Online (Sandbox Code Playgroud) 我正在使用 pyinstaller 将 python 脚本转换为 Ubuntu (14.04) 中的二进制文件。我使用 Canopy (Enthought) 来管理所有的 python 库。
代码使用了 networkx、numpy 和 scipy。这是我的规范文件:
# -*- mode: python -*-
a = Analysis(['main_test.py'],
pathex=['/home/sean/Desktop/prog',],
hiddenimports=[],
hookspath=None,
runtime_hooks=None)
pyz = PYZ(a.pure)
exe = EXE(pyz,
a.scripts,
exclude_binaries=True,
name='main_test',
debug=False,
strip=None,
upx=True,
console=True )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=None,
upx=True,
name='main_test')
Run Code Online (Sandbox Code Playgroud)
起初我得到了错误:
ImportError: libmkl_gf.so: cannot open shared object file:
No such file or directory
Run Code Online (Sandbox Code Playgroud)
然后我找到了 .so 库
/home/sean/Canopy/appdata/canopy-1.3.0.1715.rh5-x86/lib
Run Code Online (Sandbox Code Playgroud)
我手动将几个 .so 文件复制到 dist 目录中。
但是,我遇到了另一个错误:
File "/home/sean/Enthought/Canopy_32bit/User/lib/python2.7/site-
packages/PyInstaller/loader/pyi_importers.py", line 409, …Run Code Online (Sandbox Code Playgroud) line_loop 是由 opengl 创建的。我需要选择线段的一个顶点,然后将其拖动到 2D 屏幕中的某个位置。
我的想法是使用Opengl拾取方法拾取线的顶点,然后存储命中记录的缓冲区也从glSelectBuffer. 问题是我如何知道从返回缓冲区的信息中选择了哪个顶点?缓冲区存储name顶点的 。但似乎顶点在GL_RENDER模式中没有名称?
更新:还有其他方便的方法可以通过鼠标拖动线条吗?
c++ ×3
destructor ×2
arraylist ×1
c# ×1
canopy ×1
clipperlib ×1
enthought ×1
java ×1
opengl ×1
overloading ×1
polygon ×1
pyinstaller ×1
python ×1
scipy ×1
union ×1