• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

static 멤버 변수를 인스턴스 없이 호출할때

19.02.15 23:20 작성 조회수 255

0

강의 11분에서 설명하시는 질문입니다

아래는 제가 따라서 쓴 코드입니다.

include

using namespace std;

class something

{

private:

static int s_value;

int m_value;

public:

static int getValue()

{

return s_value;

}

int temp()

{

return this->s_value;

}

};

int something::s_value = 1024;

int main()

{

cout << something::getValue()<< endl;

something s1,s2;

cout << s1.getValue() << endl;

int(something::*fptr1)() = &something::temp;

cout << (s2.*fptr1)()<<endl;

int (*fptr2)() = &something::getValue();

막상 빌드를 하려고 하는데

Cannot take the address of an rvalue of type 'int'

라고 뜹니다..... 우측값의 타입이 'int' 인 주소를 넣지 마세요 라는 말인 것 같은데..

어떻게 해결해야 할지 잘 모르겠습니다ㅠㅠ

답변 2

·

답변을 작성해보세요.

0

김기상님의 프로필

김기상

2020.04.29

int (*fptr2)() = &something::getValue(); -> int (*fptr2)() = &something::getValue;

괄호를 빼셔야 합니다.

0

당황스러우시겠네요. 안타깝게도 정확히 어느 줄에서 오류가 발생한건지 모르겠네요. 저도 인간 컴파일러는 아니라서 그냥 봐서는 모릅니다. 단순히 따라하신 것이 잘 안되는 거라면 아래 영상 참고하시기 바랍니다.

똑같이 따라서 쳤는데도 에러가 나요

https://www.youtube.com/watch?v=0OBG_cpJQOo&t=12s

채널톡 아이콘