0

嗨,我是一名初级程序员,我一直在研究的程序有问题。重点是创建一个单独的类,该类具有一个由随机生成的数字组成的数组,作为一个私有变量,数组的大小,以及通过使用数组表示个体适应度的数字。

它利用头文件、头文件的源文件和测试头文件的源文件。出于某种原因,每当我尝试编译时,都会遇到断点,而 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;
}

我已经尝试寻找可能的解决方案(添加了复制构造函数和析构函数),但似乎没有任何解决问题的方法。任何帮助将非常感激。

4

1 回答 1

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();
}
于 2013-02-09T01:54:57.457 回答