2013-02-09 80 views
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; 
} 

我试图寻找可能的解决方案(添加的拷贝构造函数和析构函数)和似乎没有任何解决问题。 任何帮助将不胜感激。

回答

1

要分配双

变化的数组:

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

TO

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

您的代码超支内存时,你只分配一张双人床和写入数据到内存

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