小编Lis*_*isa的帖子

如何在C++中使用for循环创建多个对象?

我试图使用for循环创建多个对象,因为最终我希望这个程序根据我的输入创建不同数量的类.我试着用前一个问题的答案来写这个.但是,当我尝试编译时,我得到错误'没有匹配函数来调用'Genes :: Genes()'

#include <iostream>
#include <cstdlib>
#include <ctime> 

using namespace std;

float random();

class Genes{
 public:
 double cis;
 double coding;
 double effect;
 Genes(double a, double b, double c);
};

Genes::Genes(double a, double b, double c) 
{
  cis=a;
  coding=b;
  effect=c;
};

int main()
{
  int geneno, i;

  srand(time(NULL));

  geneno=4; //this will probably be cin later

  Genes *genes=new Genes[10]

  for(i=0;i<=geneno;i++){
    double d,e,f;

    d=random();
    e=random();
    f=random();

    genes[i]=Genes(d,e,f);

    cout<<"cis is "<<genes.cis<<'\n';
    cout<<"coding is "<<genes.coding<<'\n';
    cout<<"Effect for gene is "<<genes.effect<<'\n';

    }
 delete[] …
Run Code Online (Sandbox Code Playgroud)

c++ object

9
推荐指数
2
解决办法
3万
查看次数

使用fetchrow_hashref存储数据

我试图从MySQL数据库中获取信息,然后我将在perl中操作:

use strict;
use DBI;

my $dbh_m= DBI->connect("dbi:mysql:Populationdb","root","LisaUni") 
or die("Error: $DBI::errstr");

my $Genotype = 'Genotype'.1;
#The idea here is eventually I will ask the database how many Genotypes there are, and then loop it round to complete the following for each Genotype:

my $sql =qq(SELECT TransNo, gene.Gene FROM gene JOIN genotypegene ON gene.Gene =       genotypegene.Gene WHERE Genotype like '$Genotype');
my $sth = $dbh_m-> prepare($sql);
$sth->execute;

my %hash;

my $transvalues = $sth->fetchrow_hashref;
my %hash= %$transvalues;

$sth ->finish();
$dbh_m->disconnect();       

my $key;
my …
Run Code Online (Sandbox Code Playgroud)

mysql perl hash

4
推荐指数
1
解决办法
6104
查看次数

Perl中的Randomise/Shuffle哈希键

我在不改变哈希内容的情况下不时地运行哈希.我希望哈希每次都以随机顺序迭代(我知道哈希的内部顺序是随机的,但我需要随机顺序来改变)

我想要的代码如下:

 for (my $i=1; $i<=$PopulationsizeA;$i++){ 
    my $P1 = rand;
    my $total=0; 
    my $Parent1='Parent1';
    my $P1A;
while ((my $Genotype1, my $Fitness1)=each (%Normalisedfithash)){
        $P1A=$P1-$total;

        if ($Parent1 eq 'Parent1'){ 
            if ($P1A<=$Fitness1){

                $Parent1=$Genotype1;
                $P1Tallyhash{$Genotype1}+=1; 

            }
            else{
                $total+=$Fitness1;
            }
        }
    }
  }
Run Code Online (Sandbox Code Playgroud)

我需要%Normalisedfithash的顺序随机更改for循环的每次迭代.我看到了类似的问题,但是我不希望值随机更改键,而是希望键保持其关联值,但随机更改顺序.

谢谢!

random perl hash

3
推荐指数
2
解决办法
2106
查看次数

MySQL错误1452 - 无法插入数据

我将一些数据插入以下MySQL表:

CREATE TABLE genotype
(
Genotype VARCHAR(20),
Fitness FLOAT NULL,
Tally INT NULL,
PRIMARY KEY (Genotype)
)ENGINE=InnoDB;


CREATE TABLE gene
(
Gene VARCHAR(20),
E FLOAT NOT NULL,
Q2 FLOAT NOT NULL, 
PRIMARY KEY (Gene)
)ENGINE=InnoDB;

CREATE TABLE genotypegene
(
Genotype VARCHAR(20),
Gene VARCHAR(20),
FOREIGN KEY (Genotype) REFERENCES genotype(Genotype),
FOREIGN KEY (Gene) REFERENCES gene(Gene)
)ENGINE=InnoDB;
Run Code Online (Sandbox Code Playgroud)

我首先将数据插入基因型/基因,但在尝试插入基因型时会出现以下错误:

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`populationdb`.`genotypegene`, CONSTRAINT `genotypegene_ibfk_2` FOREIGN KEY (`Gene`) REFERENCES `gene` (`Gene`))
Run Code Online (Sandbox Code Playgroud)

我在这个表中插入的数据是:Genotype1,Gene1 …

mysql foreign-keys mysql-error-1452

2
推荐指数
1
解决办法
5806
查看次数

标签 统计

hash ×2

mysql ×2

perl ×2

c++ ×1

foreign-keys ×1

mysql-error-1452 ×1

object ×1

random ×1