重载的构造函数在调用时会导致核心转储

时间:2014-03-03 18:40:11

标签: c++ constructor-overloading

我在编写的程序中有三个用户定义的类:ConceptClass,GraphRep和Hamming。我正在尝试使用具有“ConceptClass”类型的单个参数的构造函数来构造汉明对象。在Hamming对象的特定构造函数中,应使用具有“int”类型的单个参数的构造函数创建GraphRep对象。

以下是代码:

GraphRep.H:

#include <iostream>
#include <math.h>
#include "ConceptClass.h"
#include "ConceptClassWriter.h"

#ifndef __GRAPHREP_H
#define __GRAPHREP_H

using namespace std;

class GraphRep
{
private:

  //Size of matrix
  int n;

  //Number of Instances
  int m;

  //Boolean adjacency matrix
  int* boolMat;

  //Weighted adjacency matrix
  int* weightMat;

  //numer of vertices
  int verts;

  //number of edges
  int edges;

  //Degree Sequence
  int* degSeq;

  //Associated Concept Class
  ConceptClass conc;


public:

    //Default constructor
    GraphRep();

    //Constructor for empty graph with n vertices
    GraphRep(int nn);

    //Copy constructor
    GraphRep(const GraphRep& original);

    //Destructor
    ~GraphRep();

};

#endif

GraphRep.cpp:

#include "GraphRep.h"

GraphRep::GraphRep()
{
    n = 0;
    m = 0;

    boolMat = new int[n*n];
    weightMat = new int[n*n];
    verts = 0;
    edges = 0;
    degSeq = new int[n];

   for (int i=0;i<n;i++)
      {
    for (int j=0;j<n;j++)
      {
        boolMat[i*n+j] = 0;
        weightMat[i*n+j] = 0;
      }

    degSeq[i] = 0;
      }

}

GraphRep::GraphRep(int nn)
{
  n = nn;
  m = 0;

  boolMat = new int[n*n];
  weightMat = new int[n*n];

  verts = n;
  edges = 0;
  degSeq = new int[n];

   for (int i=0;i<n;i++)
      {
    for (int j=0;j<n;j++)
      {
        boolMat[i*n+j] = 0;
        weightMat[i*n+j] = 0;
      }

    degSeq[i] = 0;
      }

}

GraphRep::GraphRep(const GraphRep& original)
{
  n = original.n;
  m = original.m;

  boolMat = new int[n*n];
  weightMat = new int[n*n];

  verts = original.verts;
  edges = original.edges;
  degSeq = new int[n];

  for (int i=0;i<n;i++)
    {
      for (int j=0;j<n;j++)
    {
      boolMat[i*n+j] = original.boolMat[i*n+j];
      weightMat[i*n+j] = original.boolMat[i*n+j];
    }

      degSeq[i] = original.degSeq[i];
    }


}

GraphRep::~GraphRep()
{
  //delete[] boolMat;
  //delete[] weightMat;
  //delete[] degSeq;
}

Hamming.h:

#ifndef HAMMING_H
#define HAMMING_H

#include <iostream>
#include <math.h>
#include "ConceptClass.h"
#include "GraphRep.h"

using namespace std;

class Hamming
{
 private:
  ConceptClass conc;

  int n;

 public:
  Hamming();

  Hamming(ConceptClass C);

  ~Hamming();

};


#endif

Hamming.cpp:

#include "Hamming.h"

Hamming::Hamming()
{
  n = 0;
}

Hamming::Hamming(ConceptClass C)
{
  conc = C;
  n = C.getN();

  GraphRep g1(n);

}

Hamming::~Hamming()
{
}
main.cpp的

部分:

#include<iostream>
#include<list>
#include "ConceptClass.h"
#include "GraphRep.h"
#include "Hamming.h"

using namespace std;   

... 

 //Load the concept classes from a file
list<ConceptClass> classes = load.loadConceptClasses(argv[2]);

 for ( it=classes.begin(); it != classes.end(); it++ )
 {
Hamming H(*it);
 }

当我尝试运行代码时,出现以下错误:

  

*检测到glibc vcdrtd:free():指针无效:0x0000000000402390 * *   ======= Backtrace:========= / lib64 / libc.so.6 [0x3e0ee76166] vcdrtd [0x40bff0] vcdrtd [0x41b7d2] vcdrtd [0x41f6b3] vcdrtd [0x4031f1]   /lib64/libc.so.6(__libc_start_main+0xfd)[0x3e0ee1ed1d]   vcdrtd [0x4023b9]   =======内存映射:======== 00400000-0042d000 r-xp 00000000 00:18 194332023 / home / zermelo / melocher / temp2 / vcdrtd   0062c000-0062e000 rw-p 0002c000 00:18 194332023
  / home / zermelo / melocher / temp2 / vcdrtd 01bfb000-01c1c000 rw-p 00000000   00:00 0 [堆] 3e0ea00000-3e0ea20000   r-xp 00000000 08:01 554121 /lib64/ld-2.12.so   3e0ec1f000-3e0ec20000 r - p 0001f000 08:01 554121
  /lib64/ld-2.12.so 3e0ec20000-3e0ec21000 rw-p 00020000 08:01 554121
  /lib64/ld-2.12.so 3e0ec21000-3e0ec22000 rw-p 00000000 00:00 0   3e0ee00000-3e0ef8b000 r-xp 00000000 08:01 554124
  /lib64/libc-2.12.so 3e0ef8b000-3e0f18a000 --- p 0018b000 08:01 554124
  /lib64/libc-2.12.so 3e0f18a000-3e0f18e000 r - p 0018a000 08:01 554124
  /lib64/libc-2.12.so 3e0f18e000-3e0f18f000 rw-p 0018e000 08:01 554124
  /lib64/libc-2.12.so 3e0f18f000-3e0f194000 rw-p 00000000 00:00 0   3e0f200000-3e0f283000 r-xp 00000000 08:01 554128
  /lib64/libm-2.12.so 3e0f283000-3e0f482000 --- p 00083000 08:01 554128
  /lib64/libm-2.12.so 3e0f482000-3e0f483000 r - p 00082000 08:01 554128
  /lib64/libm-2.12.so 3e0f483000-3e0f484000 rw-p 00083000 08:01 554128
  /lib64/libm-2.12.so 3e14600000-3e14616000 r-xp 00000000 08:01 554155
  /lib64/libgcc_s-4.4.7-20120601.so.1 3e14616000-3e14815000 --- p   00016000 08:01 554155
  /lib64/libgcc_s-4.4.7-20120601.so.1 3e14815000-3e14816000 rw-p   00015000 08:01 554155
  /lib64/libgcc_s-4.4.7-20120601.so.1 3e15a00000-3e15ae8000 r-xp   00000000 08:01 1373404
  /usr/lib64/libstdc++.so.6.0.13 3e15ae8000-3e15ce8000 --- p 000e8000   08:01 1373404 /usr/lib64/libstdc++.so.6.0.13   3e15ce8000-3e15cef000 r - p 000e8000 08:01 1373404
  /usr/lib64/libstdc++.so.6.0.13 3e15cef000-3e15cf1000 rw-p 000ef000   08:01 1373404 /usr/lib64/libstdc++.so.6.0.13   3e15cf1000-3e15d06000 rw-p 00000000 00:00 0 7f564db0a000-7f564db0f000   rw-p 00000000 00:00 0 7f564db39000-7f564db3c000 rw-p 00000000 00:00 0   7fff92840000-7fff92855000 rw-p 00000000 00:00 0
  [stack] 7fff92896000-7fff92897000 r-xp 00000000 00:00 0
  [vdso] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0
  [vsyscall] Abort(核心倾销)

似乎具有Concept Class类型的Hamming构造函数工作正常,但GraphRep g1(n)出现问题;从汉明建设者那里打电话。如果我将其更改为GraphRep g1();,它可以正常工作,如果我将其更改为GraphRep g1;它也会导致核心转储。

我没有最丰富的动态内存管理经验,所以我猜这个问题与析构函数有关。您会注意到GraphRep析构函数中的删除已被注释掉。我已尝试使用那些删除运行它,但遇到了同样的问题。

似乎问题是由于我的main函数中有一个循环这个事实。通常可以构建一个Hamming对象,但是在第一次迭代结束时就会出现问题。

如果您对我有任何建议,或者需要更多信息,请告诉我。

谢谢!

编辑:对不起,它应该是GraphRep g1(n);同样的问题,我只是尝试不同的东西,忘了改变它的帖子。 GraphRep g1(n);应该通过调用GraphRep(int nn)构造函数来创建GraphRep类型的对象。

编辑:我也试过完全注释掉GraphRep(int nn)构造函数的内容,所以jsut就变成了这个:

GraphRep::GraphRep(int nn)
{
}

这仍然会出错。不知道我对这个特殊的重载构造函数做错了什么。

0 个答案:

没有答案