我試圖創建一個列表(這是一個對象的私人memeber)對象的指針...創建對象的指針列表
所以我編碼方案是兩個組之間的模擬「英雄「S。每個英雄都有特殊的力量,物理屬性等等和一系列目標。目標列表是指向Hero的指針列表。當我調用構造函數創建一個Hero時,除了目標列表之外,所有信息都用隨機值初始化。到目前爲止,我已經創建了兩個Hero's,一隊和二隊的名單。我試圖在隊1中創建一個指向隊列2的指針列表,反之亦然。 main()中的for循環系列是我擁有的最佳解決方案,但函數Hero :: setTarget「push_back」多次使用第一個Hero的地址。任何建議將不勝感激。
這裏是我迄今排除Powers.h和Powers.cpp文件...
編輯:刪除嵌套的for循環,但我仍然得到傳遞到我的列表中的同一個地址....
#include <iostream>
#include <list>
#include <random>
#include <ctime>
#include "Hero1.h"
#include "Powers.h"
using namespace std;
int main()
{
///random number generator
default_random_engine generator(time(NULL));
uniform_int_distribution<int> numHero(1,10);
///lists of hero's, pointers to a hero, list iterators
list<Hero> team1;
list<Hero> team2;
Hero * hptr1;
Hero * hptr2;
list<Hero>::iterator hitr1;
list<Hero>::iterator hitr2;
/// team 1 is created
int a = numHero(generator);
for(int x=0; x<a; x++)
{
hptr1 = new Hero();
team1.push_back(*hptr1);
}
/// team 2 is created
int b = numHero(generator);
for(int x=0;x<b;x++)
{
hptr2 = new Hero();
team2.push_back(*hptr2);
}
for(hitr2=team2.begin();hitr2!=team2.end();hitr2++)
{
hptr1->setTarget(hptr2);
}
for(hitr1=team1.begin();hitr1!=team1.end();hitr1++)
{
hptr2->setTarget(hptr1);
}
///printing results for list of targets
int w =1;
cout<<"target address "<<w<<endl;
hitr1=team1.begin();
hptr1->displayTarget();
英雄是我的Hero.h
#ifndef HERO1_H_INCLUDED
#define HERO1_H_INCLUDED
#include <iostream>
#include <random>
#include <ctime>
#include <list>
#include "Powers.h"
using namespace std;
class Power;
class Hero
{
public:
Hero();
// ~Hero();
void setID();
void setLoc();
void setPhy();
void setPowers();
void setEquip();
void setTarget(Hero *h);
int getID(){return id;}
int getLoc(int x);
int getPhy(int x);
void displayHero();
void displayTarget();
void displayPowers();
private:
int id;
int location[3];
int physical [4];
Power * powPtr;
Power * equipPtr;
Hero * targetPtr;
list<Power> powers;
list<Power> equipment;
list<Hero*> target;
list<Power>::iterator equipItr;
list<Power>::iterator powItr;
list<Hero*>::iterator targetItr;
};
我Hero.cpp
#include "Hero1.h"
default_random_engine generator(time(NULL));
uniform_int_distribution<int> distribution(100000,200000);
normal_distribution<double> disto(50,10);
uniform_int_distribution<int> randPow(1,3);
Hero::Hero()
{
setLoc();
setID();
setPhy();
setPowers();
setEquip();
}
void Hero::setLoc()
{
location[0] = 1;
location[1] = 2;
location[2] = 3;
}
void Hero::setID()
{
int a = distribution(generator);
id = a;
}
void Hero::setPhy()
{
double a = disto(generator);
double b = disto(generator);
double c = disto(generator);
double d = disto(generator);
physical[0] = a;
physical[1] = b;
physical[2] = c;
physical[3] = d;
}
void Hero::setPowers()
{
int a = randPow(generator);
for(int x=0;x<a;x++)
{
powPtr = new Power(getPhy(3));
powers.push_back(*powPtr);
}
}
void Hero::setEquip()
{
int a = randPow(generator);
for(int x=0;x<a;x++)
{
equipPtr = new Power(getPhy(3));
powers.push_back(*equipPtr);
}
}
void Hero::setTarget(Hero *h)
{
target.push_back(h);
}
void Hero::displayTarget()
{
int x =1;
for(targetItr=target.begin();targetItr!=target.end();targetItr++)
{
cout<<&targetPtr<<endl;
x++;
}
cout<<x<<endl;
}
int Hero::getLoc(int x)
{
int p;
p = location[x];
return p;
}
int Hero::getPhy(int x)
{
int p;
p = physical[x];
return p;
}
void Hero::displayPowers()
{
cout<<"Number of powers = "<<powers.size()<<endl<<endl;
for(powItr=powers.begin();powItr!=powers.end();powItr++)
{
powPtr->displayEffect();
}
}
void Hero::displayHero()
{
cout<<"Id :\t\t\t\t\t"<<id
<<"\nLocation:\t\t\t\t"<<location[0]<<","<<location[1]<<","<<location[2]
<<"\nPhysical attributes:\tstrength\t"<<physical[0]<<"\n\t\t\tendurance\t"<<physical[1]
<<"\n\t\t\tagility\t\t"<<physical[2]<<"\n\t\t\tspeed\t\t"<<physical[3]<<endl<<endl;
displayPowers();
}
我試着運行你提供的代碼,但我仍然有一個地址被重複隨機次數。 我無法在這裏發佈代碼,所以看看下一個答案... – bryan