빨간색 에러들

[C++] error: ISO C++ does not permit :: initialize the protected static member / undefined reference static member

vhxpffltm 2022. 5. 1. 21:02
반응형

C++을 공부하는데 static 변수가 좀 특이한것을 확인할 수 있다.

 

특히, static 변수의 경우 컴파일러에 따라 특징을 나타낼수 있었다.

 

VS에서는 inline 키워드를 함부로 사용할 경우 컴파일 에러를 나타낸다.

 

하지만, GCC에서는 컴파일 에러가 나타나지 않는다.

 

또한, GCC에서는 표준에 따라 차이가 있는데

 

  • static 키워드의 정의를 할수 있는가? 초기화는?
  • class 내부에 선언된 static 변수를 사용할때 어떻게 해야하는가..?

 

아래 코드가 VS에서 그리고 Gcc c++17 문법에서 inline이 있을때와 없을때 어떻게 되는지 확인할 수 있다.

 

간단히 보면 GCC에서는 inline 키워드로 static 변수를 class 선언 파일에서 바로 정의가 가능하다.

VS에서는 inline 키워드를 비정적 데이터에 사용할 수 없는 문제가 있는데..  컴파일 옵션 등을 살펴보면 좋을 것 같다.

inline 키워드를 제거하고 다른 소스파일에 static 변수를 정의하고 사용하는 것이 일반적이다.

 

 

 

아래 링크에서 좀 더 구체적인 해답을 생각할 수 있다.

 

 

 

https://stackoverflow.com/questions/29799766/initialize-the-protected-static-member-from-the-subclass

 

Initialize the protected static member from the subclass

I want to know if it's possible to initialize a protected static member from the subclass. For example, // head file class Test { protected: static int i; }; class Test2 : public Test{}; //cp...

stackoverflow.com

https://stackoverflow.com/questions/9110487/undefined-reference-to-a-static-member

 

Undefined reference to a static member

I'm using a cross compiler. My code is: class WindowsTimer{ public: WindowsTimer(){ _frequency.QuadPart = 0ull; } private: static LARGE_INTEGER _frequency; }; I get the following error...

stackoverflow.com

 

반응형