2014-02-22 2 views
0

헤더크기가 0을 반환합니까?

#ifndef INTVECTOR_H 
#define INTVECTOR_H 

using namespace std; 
class IntVector{ 
private: 
    unsigned sz; 
    unsigned cap; 
    int *data; 
public: 
    IntVector(); 
    IntVector(unsigned size); 
    IntVector(unsigned size, int value); 
    unsigned size() const; 
}; 
#endif 

바디

#include "IntVector.h" 
#include <iostream> 
#include <algorithm> 
#include <cstring> 
using namespace std; 



IntVector::IntVector(){ 
    sz = 0; 
    cap = 0; 
    data = NULL; 
} 

IntVector::IntVector(unsigned size){ 
    sz = size; 
    cap = size; 
    data = new int[sz]; 
    *data = 0; 
} 

IntVector::IntVector(unsigned size, int value){ 
    sz = size; 
    cap = size; 
    data = new int[sz]; 
    for(unsigned int i = 0; i < sz; i++){ 
     data[i] = value; 
    } 
} 

unsigned IntVector::size() const{ 
    return sz; 
} 

내가, 홈페이지 내 기능을 테스트 (IntVector (6, 4); < < testing.size() < < ENDL cout을) 내를 testing.size() 테스트는 IntVector 함수에서 sz와 cap을 지정하면 이론적으로 6 일 때 일관되게 0을 출력합니다. 왜 0을 출력하는지에 대한 아이디어가 있습니까? 여기에 폐기되는 임시을 만드는 것처럼

+1

이것이'main()'에있는 경우 :'IntVector (6, 4);','testing'이 어디로 들어 왔는지 알고 싶습니다. – WhozCraig

답변

3

가 보이는 :

IntVector(6, 4); 

는 당신과 같이 개체를 만들려면 :

IntVector testing(6, 4); 

그런 다음 works을.

+0

나는 본다. 나는 IntVector 테스트를 받았다. 하지만 당신이 그렇게 할 때 비주얼 스튜디오는 그것을 좋아하지 않는다고 생각합니다. – user3314899

+0

@ user3314899 Visual Studio 때문이 아닙니다. –

관련 문제