동적으로 개체 배열을 만든 다음 배열의 개체 멤버 함수를 사용하려고합니다. 두 가지 조합을 시도했지만 배열에있는 객체의 표시 멤버를 호출하려고하면 프로그램이 중단됩니다.개체 배열의 멤버 함수에 액세스
Something * pSomethings[12] = { NULL }; //pointer to array of 12 Somethings
to load the array I use:
Something * pSomething; //create a temp pointer of Something
pSomething = pSomethings[0]; //assign temp pointer to first item in array
pSomething = new Widget(size, weight);//pSomthings[0] should be a new widget
cout << pSomething->getSize(); //seeing if data member was set (shows correct)
cout << pSomething->getWeight(); //seeing if data member was set (shows correct)
when I try to invoke the following my program breaks:
Something::display(){
Something * pSomething; //create another local temp pointer
pSomething = psomethings[0];//assign temp pointer to initialized pSomthing above
cout << pSomething->getSize(); // <---- breaks if run in main.
}
나는 C++와 내가 매우 간단한 실수를 할 수 일반적으로 포인터에 새로 온 사람 만의 getSize()는 첫 번째 함수에 있지만 화면에 제대로 표시하는 이유는 단순히 내가 작성) (이해가 안 돼요 모든 것이 똑같습니다 (임시 포인터, 동일한 인덱스에 할당 됨 등). 그러면 오류가 발생합니다.
pSomething = pSomethings[0]; //assign temp pointer to first item in array
pSomething = new Widget(size, weight);//pSomthings[0] should be a new widget
당신이 pSomething
에 new
에 의해 반환 된 값을 할당으로 마지막 코멘트가 잘못된, 하지pSomethings[0]
에 :
하지 * 12 일도의 배열에 대한 포인터 *. –