我有一个像这样的头文件
#ifndef __coulomb_h_
#define __coulomb_h_
#include "nml_dcvector.h"
#include <fstream>
#include <iostream>
#include <complex>
#include <vector>
using namespace std;
class Coulomb{
public:
typedef complex<double> Complex_t;
Coulomb(int na, int g1, int g2, int o)
: numAtoms(na), g_min(g1), g_max(g2), o_max(o)
{
o_total=o_max*o_max;
_H_coul = new complex<double>[1];
_H_exch = new complex<double>[1];
}
~Coulomb()
{delete [] _H_coul; delete [] _H_exch;}
private:
complex<double>* _H_coul;
complex<double>* _H_exch;
然后在我的.cpp文件中写下这一行
int main(int argc, char** argv) {
int rank=0, size=1;
#if (defined MPI3d && !defined FAKE_MPI)
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
#endif
char data[123];
int NumElectrons = 0;
int NumHoles = 0;
int NumAtoms = 0;
int NumOrbitals = 0;
int* eList;
int* hList;
..............
..............
int bat= NumAtoms/size;
int residue = NumAtoms%size;
int myAtoms_begin, myAtoms_end;
if(rank<residue){
myAtoms_begin = rank*(bat+1);
myAtoms_end = myAtoms_begin + bat + 1;
}
else {
myAtoms_begin = residue*(bat+1) + (rank - residue)*bat;
myAtoms_end = myAtoms_begin + bat;
}
if(ComputeCoulomb) {
Coulomb data_coulomb =
Coulomb(NumAtoms, myAtoms_begin, myAtoms_end, NumOrbitals);
这给出了分段错误。我想这是由于三个规则。然后我添加了这部分;复制构造函数和赋值构造函数
Coulomb (const Coulomb& v):numAtoms(v.numAtoms), g_min(v.g_min),
g_max(v.g_max), o_max(v.o_max)
{
o_total= v.o_total;
_H_coul = new complex<double>[1];
_H_exch = new complex<double>[1];
}
Coulomb& operator= (const Coulomb& v)
{
int o_total= v.o_total;
complex<double>* _H_coul_temp = new complex<double>[1];
std::copy(v._H_coul,v._H_coul+1, _H_coul_temp ) ;
complex<double>* _H_exch_temp = new complex<double>[1];
std::copy(v._H_exch,v._H_exch+1, _H_exch_temp );
delete [] _H_coul; delete [] _H_exch;
_H_coul= _H_coul_temp;
_H_exch= _H_exch_temp;
return *this;
}
仍然无法正常工作。请有人帮我解决这个问题。感谢
答案 0 :(得分:0)
#include <iostream>
#include <string>
//#include "nml_dcvector.h"
#include <fstream>
#include <complex>
#include <vector>
using namespace std;
class Coulomb {
public:
int numAtoms, g_min, g_max, o_max, o_total;
typedef complex<double> Complex_t;
Coulomb(int na, int g1, int g2, int o) :
numAtoms(na), g_min(g1), g_max(g2), o_max(o) {
o_total = o_max * o_max;
cout << "Inside normal constructor" << endl;
_H_coul = new complex<double> [1];
_H_exch = new complex<double> [1];
}
Coulomb(const Coulomb& v) :
numAtoms(v.numAtoms), g_min(v.g_min), g_max(v.g_max), o_max(v.o_max) {
o_total = v.o_total;
cout << "Inside copy constructor" << endl;
_H_coul = new complex<double> [1];
_H_exch = new complex<double> [1];
}
Coulomb& operator=(const Coulomb& v) {
o_total = v.o_total;
complex<double>* _H_coul_temp = new complex<double> [1];
std::copy(v._H_coul, v._H_coul + 1, _H_coul_temp);
complex<double>* _H_exch_temp = new complex<double> [1];
std::copy(v._H_exch, v._H_exch + 1, _H_exch_temp);
delete[] _H_coul;
delete[] _H_exch;
cout << "Inside assignment overloader" << endl;
_H_coul = _H_coul_temp;
_H_exch = _H_exch_temp;
return *this;
}
~Coulomb() {
delete[] _H_coul;
delete[] _H_exch;
}
private:
complex<double>* _H_coul;
complex<double>* _H_exch;
};
int main() {
Coulomb data_coulomb = Coulomb(1000, 5, 500, 20);
Coulomb* data_coulomb_2 = new Coulomb(1000, 5, 500, 0);
delete data_coulomb_2;
std::cout << "The Program is terminating!!" << endl;
}
这对我来说很好。
答案 1 :(得分:-1)
尝试使用析构函数new
。
public:
~Coulomb()
{
delete _H_coul;
delete _H_exch;
}