(C++) 클래스 배열에서 멤버 연산자 사용 디버깅 질문 (base operand of '->' is not a pointer)
글쓴이: kimhyuns55 / 작성시간: 목, 2018/04/19 - 3:40오후
#include <iostream> #define NMEM 5 using namespace std; class CMember { private: const char* name; int num, DOB; public: CMember() {} CMember(const char* c) { name = c; } CMember(const char* c, int i, int j) { name = c; num = i; DOB = j; } void SetName(const char* c) { name = c; } void SetID(int i) { num = i; } void setDOB(int i) { DOB = i; } void Print() { cout << "Name : " << name << endl; cout << "ID : " << num << endl; cout << "DOB : " << DOB << endl; } }; int main(void) { char names[5][10] = {"Amy", "Bob", "Charles", "David", "Liz"}; int DOBs[5] = {880101, 890101, 910101, 920103, 880102}; CMember m1("Kim Seon-Suk"); CMember m2("Max", 1, 931208); CMember *memlist = new CMember[5]; m1.Print(); m2.Print(); for(int i = 0; i < NMEM; i++) { *(memlist+i) -> SetName(names[i]) -> SetID(i); memlist+i -> setDOB(DOBs[i]) -> Print(); } }
에서
*(memlist+i) -> SetName(names[i]) -> SetID(i); memlist+i -> setDOB(DOBs[i]) -> Print();
의 두 부분이 문제입니다
두 줄의 경우 모두 base operand of '->' is not a pointer 에러가 발생하는데
base operand 를 어떻게 지정해야 에러를 없앨 수 있는지 질문드립니다
(두 줄 모두 목적하는 바는 같습니다만, 제 나름 디버깅을 해보려고 경우를 두개로 나눈것이므로 차이에 별다른 의미는 없습니다)
도움주시면 감사하겠습니다 :D
Forums:
-> 을 쓰실때는 왼쪽이 포인터이어야하므로
-> 을 쓰실때는 왼쪽이 포인터이어야하므로
1.
(memlist+i)->SetName(...)
(memlist+i)->setDOB(...)
이런식으로 해야합니다. 위 식에서 memlist+i 는 '+' 연산의 결과가 포인터이므로 -> 의 왼쪽이 포인터라는 문법에 맞습니다.
2.
두번째에 있는 -> 의 왼쪽은 함수 SetName, setDOB의 return 값이 포인터가 아니므로 유효하지 않습니다.
이런 메소드 체이닝을 쓰시려면 두 함수를 void 대신 CMember * 를 돌리는 타입으로 바꾸시고 return this; 라고 하시면 될 것 같네요.
---
http://coolengineer.com
해결되었습니다
덕분에 전부 잘 해결되었습니다
이해하기 쉬운 친절한 설명 감사드립니다!
댓글 달기