程序触发断点;不运行

时间:2013-02-09 01:44:21

标签: c++ visual-c++

嗨,我是初级程序员,我遇到过一个我一直在研究的程序问题。关键是创建一个单独的类,其中包含一个由随机生成的数字组成的数组作为一个私有变量,一个数组的大小,以及一个通过使用数组表示个体适应性的数字。

它利用头文件,头文件的源文件和源文件来测试头文件。出于某种原因,每当我尝试编译时,我都会到达断点,并且Visual Studio不会告诉我错误是什么。我怀疑它与我班级中的私人指针有关,但我不知道为什么或如何修复错误。

标题

#ifndef INDIVIDUAL_H
#define INDIVIDUAL_H

class individual
{   
    int size;
    double fitness;
    double* genotype;
public:
    individual(int pSize = 10);
    individual(const individual& copy);
    ~individual();
    double* getGenotype();
    double getFitness();
    int getSize();
    void mutation();
    void crossover(individual a);
};

#endif

标题来源

#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#define M_PI 3.14159265358979323846
#define M_E 2.71828182845904523536
#include <cmath>
#include "individual.h"

using namespace std;

double RandomFloat(double min = -32.768, double max = 32.768)
{
    min = min;
    max = max;
    unsigned int seed;
    seed = (unsigned int) time(0) + rand();
    srand(seed);
    double r = (double)rand() / (double)RAND_MAX;
    return min + r * (max - min);
}

double Fitness(double a[], int size)
{
    double fitness;
    double firstSum, secondSum;

    firstSum = 0;
    for(int i = 0; i<size; i++)
    {
        firstSum += a[i]*a[i];
    }
    firstSum /= size;

    secondSum = 0;
    for(int i = 0; i<size; i++)
    {
        secondSum += cos(2*M_PI*a[i]);
    }
    secondSum /= size;

    fitness = -20*exp(-0.2*sqrt(firstSum) - exp(secondSum) + 20 + M_E);

    return fitness;
}

individual::individual(int pSize)
{
    size = pSize;
    genotype = nullptr;
    genotype = new double(size);
    for(int i = 0; i<size; i++)
    {
        genotype[i] = RandomFloat();
    }

    fitness = Fitness(genotype,size);
}    

individual::individual(const individual& copy)
    :size(copy.size),genotype(new double[copy.size])
{    
    std::copy(copy.genotype, copy.genotype + copy.size, genotype);
}

individual::~individual()
{
    delete[] genotype;
}

double* individual::getGenotype()//returns a pointer
{
    return genotype;
}

double individual::getFitness()
{
    return fitness;
}

int individual::getSize()
{
    return size;
}

void individual::mutation()
{
    int first, second;
    double temp;
    first = (int)RandomFloat();
    second = (int)RandomFloat();

    temp = genotype[first];
    genotype[first] = genotype[second];
    genotype[second] = temp;
}

void individual::crossover(individual a)
{
    int crossPoint = size/3 - 1;

    for(int i = crossPoint; i<size; i++)
    {
        double temp1;

        temp1 = 0;
        temp1 = genotype[i];
        genotype[i] = a.genotype[i];
        a.genotype[i] = temp1;
    }

}

驱动程序来源

#include <iostream>
#include <string>
#include <stdlib.h>
#include <cmath>
#include <vector>
#include "individual.h"
#define M_PI 3.14159265358979323846
#define M_E 2.71828182845904523536

using namespace std;

int main()
{
    individual test;
    int size = test.getSize();
    cout << size << endl;

    for(int i = 0; i<size; i++)
    {
        cout << test.getGenotype()[i] << endl;
    }
    return 0;
}

我尝试过搜索可能的解决方案(添加了复制构造函数和析构函数),似乎没有解决问题的方法。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

分配双重数组

变化:

genotype = new double(size);  // this initialize one double and initialize value to size

genotype = new double[size];  // this creates an array which size is 'size'

当您只分配一个double并将数据写入内存时,您的代码会溢出内存

for(int i = 0; i<size; i++)
{
    genotype[i] = RandomFloat();
}
相关问题