2017-09-25 3 views
0

클래스 개체 (학생 및 직원)의 동적 할당 된 배열이 두 개인 있습니다. 사용자가 나이를 입력하면 연령에 따라 학생 배열 또는 직원 배열 요소를 업데이트하고 싶습니다. 하지만 아래 코드는 작동하지 않습니다. 변수 person은 일단 학생에게 배정 된 직원에게 재 할당되지 않습니다. 내가 입력 한 모든 데이터는 내가 입력 한 나이에 상관없이 학생에게만 전달됩니다. 내 코드에 어떤 문제가 있습니까? 하나의 변수를 가지고 조건 검사를 기반으로 하나 또는 다른 배열 요소를 할당하려면 어떻게해야합니까? 이 설정 일단동적으로 할당 된 배열의 요소 값 변경

#include <iostream> 
using namespace std; 

int main() 
{ 
    class info 
    { 
    public: 
     int unique_id; 
     char* hair_color; 
     int height; 
     int weight; 
    }; 

    class info* student; 
    student = new info[10]; 

    class info* staff; 
    staff = new info[10]; 

    for (int i=0; i<10;i++) 
    { 
     class info& person = student[i]; 

     int age ; 
     cout<< "enter age"<<endl; 
     cin >> age; 

     if(age > 18) 
     { 
      person = staff[i]; // This assignment doesn't work ?? 
     } 
     cout<< "enter unique_id"<<endl; 
     cin >> person.unique_id; 
     cout<< "enter height"<<endl; 
     cin >> person.height; 
     cout<< "enter weight"<<endl; 
     cin >> person.weight; 

    } 

    cout<<" Student "<<student[0].unique_id<<" "<<student[0].height<<"\" "<<student[0].weight<<endl; 
    cout<<" Staff "<<staff[0].unique_id<<" "<<staff[0].height<<"\" "<<staff[0].weight<<endl; 

    return 0; 
} 
+0

std::vector from the C++ Standard Library's container library.는 C++ 코드 같은데 사용하여이 작업을 수행하는 더 나은 방법이있다. 그렇다면 C++ 태그를 질문에 추가하십시오. 감사! –

+0

'int & rx = x;'참조가 있다면'rx = 5;'와 같이 참조에 새로운 값을 할당하면 어떻게 될 것이라고 생각합니까? 그리고 과제가 제대로 작동하지 않는다고 생각되는 곳에서 무슨 일이 일어나고 있습니까? –

+0

[참조를 다시 시드 할 수 없습니다.] (https://stackoverflow.com/questions/7713266/how-can-i-change-the-variable-to-which-ac-reference-refers) 이것은 @ ArtemyVysotsky는 암시합니다. – user4581301

답변

1

You cannot reseat a reference., 그것은 거기에 붙어과 기준을 재 할당하려는 시도는 참조 변수에 할당하는 요청으로 해석됩니다. 이 의미

person = staff[i]; 

실제로 student[i]의 별명 (다른 이름)입니다 personstaff[i]; 복사됩니다. student[i]은 사용자로부터 읽은 입력을 계속받습니다.

현재 사용중인 코드를 기준으로 가장 쉬운 방법은 참조를 다시 부착 할 수있는 포인터로 바꾸는 것입니다.

class info* person = &student[i]; // using pointer 

int age ; 
cout<< "enter age"<<endl; 
cin >> age; 

if(age > 18) 
{ 
    person = &staff[i]; // using pointer, but note: nasty bug still here 
         // You may have empty slots in staff 
} 

cout<< "enter unique_id"<<endl; 
cin >> person->unique_id; // note the change from . to -> 
.... 

하지만 주위에 방법이 있습니다. 사용할 배열을 알기 전까지 참조 생성을 지연시킬 수 있습니다. 이것은 많은 코드 주위를 뒤섞어 놓고 조심하지 않으면 배열에 사용되지 않은 요소를 남겨 둡니다.

다행히

std::vector<info> student; 
std::vector<info> staff; 

for (int i=0; i<10;i++) 
{ 
    info person; // not a pointer. Not a reference. Just a silly old Automatic 

    int age ; 
    cout<< "enter age"<<endl; 
    cin >> age; 

    // gather all of the information in our Automatic variable 
    cout<< "enter unique_id"<<endl; 
    cin >> person.unique_id; 
    cout<< "enter height"<<endl; 
    cin >> person.height; 
    cout<< "enter weight"<<endl; 
    cin >> person.weight; 

    // place the person in the correct vector 
    if(age > 18) 
    { 
     staff.push_back(person); 
    } 
    else 
    { 
     student.push_back(person); 
    } 
} 
관련 문제