2013-10-20 3 views
1
#include <iostream> 
using namespace std; 

class Assn2 
{ 
    public: 
    static void set_numberofshape(); 
    static void increase_numberofshape(); 

    private:   
     static int numberofshape22; 
}; 

void Assn2::increase_numberofshape() 
{ 
    numberofshape22++; 
} 

void Assn2::set_numberofshape() 
{ 
    numberofshape22=0; 
} // there is a problem with my static function declaration 

int main() 
{ 
    Assn2::set_numberofshape(); 
} 

컴파일 할 때 왜 undefined reference to Assn2::numberofshape22 오류가 발생합니까?정적 변수 및 정적 메서드에 대한 정의되지 않은 참조

정적 정수 : numberofshape22 및 두 가지 방법을 선언하려고합니다. 내가 잘못 뭘 오전 0

-1

방법 2 초기화의 numberofshape22에 의해

방법 1 개 증가 numberofshapes22?

답변

4

방금 ​​변수가 선언되었습니다. 당신은 그것을 정의해야

int Assn2::numberofshape22; 
2

클래스의 멤버 목록에서 정적 데이터 멤버의 선언은하지 않는 정의입니다.

one Definition Rule 당신이 정의해야 static 데이터 멤버를 존중합니다. 당신의 경우에는 당신이 선언 한 것뿐입니다.

예 : 이것에 대한

// in assn2.h 
class Assn2 
{ 
    // ... 
    private:   
     static int numberofshape22; // declaration 
}; 

// in assn2.cpp 

int Assn2::numberofshape22; // Definition 
+0

와우 ... 감사합니다. – Erutan409

관련 문제