我有一个这样的头文件
#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;
}
还是行不通。请任何人帮我解决这个问题。谢谢