为struct分配值

时间:2016-05-04 07:20:43

标签: c++ struct

我正在尝试创建一个遗传算法,但我仍然坚持为我的健康状况赋值。这是我的代码(或者至少是与问题相关的部分):

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <random>
#include <string>

const int population_size = 10;
const int number_of_variables = 7;

struct one_individual
{
std::vector<std::vector<double>>individual;;
double evaluation = 0;
double fit = 0;
};


int main()
{
// Generate random number

std::random_device rd;
std::mt19937 rng(rd());    // random-number engine (Mersenne-Twister in this case)
std::uniform_real_distribution<double> dist(-10.0, 10.0);

// Create vector that holds vectors called individual and fill size it to the amount of individuals I want to have.

one_individual individual;

for (int i = 0; i < population_size; i++)
{

    std::vector<double>variables;

    for (int j = 0; j < number_of_variables; j++)
    {
        variables.push_back(dist(rng));
    }

    individual.individual.push_back(variables);
    individual.fit = 0;
    individual.evaluation = 0;
}

// Do calculation with population. At the moment I just add up all the genes (sum) and display the sum for each individual.


for (int i = 0; i < population_size; i++)
{
    int j = 0;
    std::cout << "Organism " << i;
    individual.evaluation = individual.individual[i].at(j) + individual.individual[i].at(j + 1) + individual.individual[i].at(j + 2) + individual.individual[i].at(j + 3) +
        individual.individual[i].at(j + 4) + individual.individual[i].at(j + 5) + individual.individual[i].at(j + 6);

    individual[i].evaluation = individual.evaluation;
}

for (int i = 0; i < population_size; i++)
{
        std::cout << "Evaluation value for individual " << i << " is " << individual[i].evaluation << "\n";
}


std::cout << "\n";
return 0;
}

我遇到错误的部分在这里:

individual[i].evaluation = individual.evaluation;

我认为这是为评估变量赋值的方法,但事实并非如此。我得到的错误是:

严重级代码描述项目文件行抑制状态 错误C2676二进制'[':'one_individual'未定义此运算符或转换为预定义运算符可接受的类型慢c:\ users \ milan \ documents \ visual studio 2015 \ projects \ slow \ slow \ slow.cpp 59
'.evaluation'左边的错误C2228必须有class / struct / union慢c:\ users \ milan \ documents \ visual studio 2015 \ projects \ slow \ slow \ slow.cpp 59
错误(活动)没有运算符“[]”匹配这些操作数慢c:\ Users \ Milan \ Documents \ Visual Studio 2015 \ Projects \ Slow \ Slow \ Slow.cpp 59

如果有人能告诉我如何能够做到这一点,或者我应该特意阅读以解决这个问题,我将非常感激!

编辑:TobiasR在评论中回答,谢谢!

0 个答案:

没有答案