2014-09-09 5 views
0

나는 도전하고있는 토끼 모의 실험을하고 있습니다. 클래스 객체의지도가 있습니다. 지도의 키가 들어있는 목록도 있습니다. 시뮬레이터의 각 라운드에서 클래스에 더 많은 오브젝트를 추가하고 맵과 목록을 업데이트하려고합니다. 이렇게하기 위해 새로운 클래스 객체를 무작위로 생성하여지도에 추가하고 키를 목록에 추가하는 별도의 "생성"함수를 작성했습니다.C++ 주요 외부 기능 간 목록 공유

문제는 내가 생성 함수를 호출 한 다음 맵 또는 목록이 비어있는 (확실하지 않은) 목록을 사용하여 반복합니다. 함수를 떠나기 전에지도를 반복 할 경우 가장 새로운 객체 만 표시됩니다. 함수의 코드를 주 함수로 옮기면 올바르게 작동합니다. 코드를 두 번 호출하면 코드의 첫 번째 반복에서 생성 된 새 객체와 두 번째 반복에서 생성 된 객체가 생깁니다.

함수를 호출하고 이전 목록이나지도를 덮어 쓸 때마다 새 목록이나지도가 만들어지고 있다고 생각합니다. 주 기능과 작성 기능 사이를 통과하도록 목록과 맵을 가져 오는 방법은 무엇입니까?

#include "stdafx.h" 
#include <iostream> 
#include "windows.h" 
#include <string> 
#include <sstream> 
#include <array> 
#include <time.h> 
#include <conio.h> 
#include <map> 
#include <list> 

class Bunny 
{ 
public: 
    char fname; 
    int sex, age, color, status; 
    Bunny(int, int, int, int); 
    Bunny(); 
    int s() { return (sex); } 
    int a() { return (age); } 
    int c() { return(color);} 
    int st() { return (status);} 
    int aging(int age) { return (age + 1); } 
}; 

Bunny::Bunny(int s, int a, int c, int st) 
{ 
    sex = s; 
    age = a; 
    color = c; 
    status = st; 
} 
Bunny::Bunny(){} 

void creation(std::map<std::string, Bunny> bunnies, std::list<std::string>names, int births); 

std::string firstname(int s, int num) 
{ 
    std::string name; 
    if (s == 0) 
    { 
     switch (num) 
     { 
     case (0) : 
      name = "Tim"; 
      break; 
     case (1) : 
      name = "Tom"; 
      break; 
     case (2) : 
      name = "Mark"; 
      break; 
     case (3) : 
      name = "Bob"; 
      break; 
     case (4) : 
      name = "Rob"; 
      break; 
     } 
    } 
    if (s == 1) 
    { 
     switch (num) 
     { 
     case (0) : 
      name = "Suzy"; 
      break; 
     case (1) : 
      name = "Linda"; 
      break; 
     case (2) : 
      name = "Mary"; 
      break; 
     case (3) : 
      name = "Jan"; 
      break; 
     case (4) : 
      name = "Julie"; 
      break; 
     } 
    } 
      return (name); 
} 

void main() 
{ 

    int num = rand() % 5; 
    int n, births = 10; 
    std::list<std::string>names; 
    std::map<std::string, Bunny> bunnies; 
    srand(time(0)); 
    creation(bunnies, names, births); 
    std::cout << "Number" << "\t" << "Name" << "\t" << "age" << "\t" << "Sex" << "\t" << "Color" << "\t" << "Vampire?" "\n"; 
    n = 0; 
    for (std::list<std::string>::iterator it = names.begin(); it != names.end(); it++) 
    { 
     n++; 
     std::cout << n << "\t"; 
     std::cout << " " << *it; 
     std::cout << "\t" << bunnies[*it].a() << "\t" << bunnies[*it].s() << "\t" << bunnies[*it].c() << "\t" << bunnies[*it].st() << "\n"; 
    } 
    creation(bunnies, names, births); 
    _getch(); 
} 



/*void year() 
{ 
    for (std::list<std::string>::iterator it = names.begin(); it != names.end(); it++) 
    { 
     bunnies[*it].aging(bunnies[*it].a()) 
    } 
}*/ 

void creation(std::map<std::string, Bunny> bunnies,std::list<std::string> names,int births) 
{ 
    int n; 
    for (n = 0; n < births; n++) 
    { 
     int num = std::rand() % 5; 
     char id = (std::rand() % 100) + 20; 
     int s = std::rand() % 2; 
     std::string f = firstname(s, num) + '_' + id; 
     int a = 0; 
     int c = std::rand() % 5; 
     int st; 
     if (rand() % 50 == 43) st = 1; else st = 0; 
     bunnies[f] = Bunny(s, a, c, st); 
     names.push_front(f); 
     //std::cout << f << " " << bunnies[f].a() << " " << bunnies[f].c() << "\n"; 
    } 
    std::cout << "Number" << "\t" << "Name" << "\t" << "age" << "\t" << "Sex" << "\t" << "Color" << "\t" << "Vampire?" "\n"; 
    n = 0; 
    for (std::list<std::string>::iterator it = names.begin(); it != names.end(); it++) 
    { 
     n++; 
     std::cout << n << "\t"; 
     std::cout << *it; 
     std::cout << "\t" << bunnies[*it].a() << "\t" << bunnies[*it].s() << "\t" << bunnies[*it].c() << "\t" << bunnies[*it].st() << "\n"; 
    } 
} 
+0

에 :

이로부터 생성 기능을 변경

한 번에 복잡성을 약간 추가, 작고 간단한 시작, 개발 새로운 기능을 격리하고, 모든 단계에서 테스트하고, * 작동하지 않는 코드에는 절대 추가하지 마십시오. 여전히 문제가 발생하여 도움이 필요한 경우이 방법을 사용하면 오작동하는 코드를 [최소 완료 예] (http://stackoverflow.com/help/mcve)로 쉽게 줄일 수 있습니다. – Beta

답변

1

귀하의 문제가 주요 기능 참조로 값 대신 전달하여지도와 목록을 전달하고 있다는 것입니다 :

여기 내 코드입니다. 이는 작성한 함수가 작성한 원래의 참조가 아니라 기존의 맵 /리스트의 사본을 수신하고 있음을 의미합니다. 그런 다음 해당 복사본을 편집하는 중이므로 변경 사항이 주 기능에 반영되지 않습니다.

void creation(std::map<std::string, Bunny> bunnies, std::list<std::string>names, int births) 

코딩의 방법입니다

특정 트릭보다 더 가치
void creation(std::map<std::string, Bunny>& bunnies, std::list<std::string>& names, int births)