2013-02-09 87 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(); 
}