2011-08-21 9 views
1

다음 3 개의 코드 블록은 각각 main.cpp, static_class_array.cpp 및 static_class_array.h입니다. 그들은 동일한 유형 없습니다클래스의 static int 배열

static_class_array.cpp||In constructor 'static_array_class::static_array_class()':| 
static_class_array.cpp|5|error: cannot convert '<brace-enclosed initializer list>' to 'int' in assignment| 
||=== Build finished: 1 errors, 0 warnings ===| 


#include "static_class_array.h" 

int main() 
{ 
    static_array_class* array_class; 

    array_class = new static_array_class(); 

    delete array_class; 

    return 0; 
} 


#include "static_class_array.h" 

static_array_class::static_array_class() 
{ 
    static_array_class::array[3] = {0,1,2}; 
} 
static_array_class::~static_array_class(){} 



#ifndef STATIC_CLASS_ARRAY_H 
#define STATIC_CLASS_ARRAY_H 

class static_array_class 
{ 
    private: 

     static int array[3]; 

    public: 

    static_array_class(); 
    ~static_array_class(); 
}; 
#endif 

답변

2

내 생각이다 :

제출 된 코드에서 오류 메시지

"cannot convert 'brace-enclosed initializer list' to 'int' in assignment"

의 는

static_array_class::static_array_class() 
    { 
    } 
    static_array_class::~static_array_class(){} 

    int static_array_class::array[3] = {0,1,2}; 

설명.

이다 코드 이유는

static_array_class::array[3] = {0,1,2}; 

{0,1,2}가 배열 요소 3 할당되어야한다는 것을 의미하는 것으로 해석된다. 3 요소는 입력 int이다 (그리고 또한 네번째 요소 인 할당되지 않음), 따라서 이것은 같다 :

int i = 0; 
i = {0,1,2}; 

따라서 에러 메시지.

+0

은 충돌하는 선언이 있다고 말했습니다. – pandoragami

2

; 나는 다음과 같은 오류를 받고 있어요

클래스는 배열을 포함하는 클래스입니다. 다른 클래스는 단지 배열입니다. 당신은 다른 모든 정적과 마찬가지로, 클래스 외부의 실제 인스턴스를 선언 할 필요가 클래스 멤버의 정적 정의와

, 당신이 구현 파일에 원하는 것은

int static_array_class::array[3] = {0,1,2}; // note this line is outside the constructor 

static_array_class::static_array_class() 
{ 
} 
static_array_class::~static_array_class(){} 
+0

내 생각인가? 그렇다면이 작업을 수행 할 수 없다는 것을 의미합니다. – pandoragami

+0

코딩과 함께 가능합니다. 좀 더 필요합니다. 클래스의 정적 멤버는 수축자가 아니라 클래스 외부에서 초기화됩니다. 사실상 구속/해체를 부분적으로 받아들이는 것만이 그것들을 클래스가 아닌 이름 공간의 일부로 거의 간주 할 수 있습니다. – Soren

+0

또한이 Q & A를 참조하십시오 : http://stackoverflow.com/questions/185844/initializing-private-static-members – Soren