gpt4 book ai didi

c++ - 程序触发断点;不运行

转载 作者:行者123 更新时间:2023-11-28 07:46:43 25 4
gpt4 key购买 nike

您好,我是一名初级程序员,我对我一直在开发的程序有疑问。重点是创建一个个人类,该类具有一个数组,该数组由作为一个私有(private)变量的随机生成的数字组成,数组的大小,以及一个表示个人通过使用该数组的适应性的数字。

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

标题

#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;
}

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

最佳答案

分配一个double数组

改变:

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();
}

关于c++ - 程序触发断点;不运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14784080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com