2012-10-16 2 views
-1

테이블을 만들 수있는 벡터 클래스를 사용하여 에뮬레이션하는 테이블이 필요한 프로그램을 작성하고 있습니다. 그러나 vec_data 제외한 클래스 테이블의 항목을 볼 수 없습니다. 이 클래스의 공용 멤버에 액세스 할 수없는 이유는 무엇입니까? 어떤 이유로 MSVC++ Intellisense는 vec_data 만 볼 수 있습니다. 예를 들어클래스가 공용 멤버에 액세스 할 수 없습니다. C++

template<class T> 
class table 
{ 
    private: 
    T* vec_data;// initialize T 

    struct tblarray : public T 
    { 
     std::vector<T> vecTbl[]; 
     bool operator[](unsigned int i) { return vecTbl[i]; } //redefine operator[] to accept unsigned int 
     static void operator new(double n) //redefine new operator 
     { 
      void *d; 
      if(n < 0) throw std::exception("Invalid Allocation to Negative number!"); 
      if(assert((d=malloc(n)) != 0) = 0) throw std::bad_alloc; 
      return d; 
     } 
     void operator delete(void *d) //redefine delete operator 
     { 
     if(assert((free(p))) = 0) throw std::exception("Invalid Free of specified data!"); 
     } 
     tblarray(const T&, unsigned int size) : T //one constructor 
     { 
     vecTbl = this.new std::vector<T>[reinterpret_cast<double>(size)]; 
     } 
     ~tblarray() //one destructor 
     { 
     this.delete(vecTbl); 
     } 
} 
public: 
    table(const T&, unsigned int size) : T 
    { 
     this.tblarray.tblarray(T, size); 
    } 
    ~table() 
    { 
     this.tblarray.~tblarray(); 
    } 
} 

:

table<int> tblOne; //legal 
table.table(int, 123); //not legal(probably not legal anyways, but intellisense says the function does not exist?) 
+0

'table (const T &, unsigned int size)'은 생성자이며 생성자는 이름이 지정되지 않고 직접 호출 할 수 없습니다. (소멸자도 공식적으로 이름이 붙지 않지만 직접 호출 할 수 있습니다.) 시간의 99.9 %를 차지하지 않아야합니다. –

+0

하지만이 작업은 다음과 같습니다. 표 * tblOne = new 표 (123) throws : 적절한 생성자가 int에서 int로 변환되지 않습니다. 테이블 비록 constructor에 int 타입의 보조 param을 가지고 있습니다. – 7c00h

+1

나는 당신이 무엇을하고 있는지 전혀 모른다고 생각합니다.'table (const T &)'는 복사 생성자이고'table (unsigned int)'는 변환 생성자가됩니다. 가지고있는 것은 정상적인 두 매개 변수 생성자입니다. 그것을 사용하려면 _two_ 매개 변수를 전달해야합니다. 하나는'table'이고, 하나는'unsigned int'입니다. –

답변

0

table.table(1,123) 불법입니다. 당신은 생성자를 그렇게 호출 할 수 없다. 당신은 구조체 선언 후 세미콜론을 놓친

+0

그러나 질문은 내 생성자 호출의 적법성에 관한 것이 아니라 왜 내가 왜 액세스 할 수 있는지에 대한 것입니다. – 7c00h

+0

왜 그럴 수 없습니까? Intellisense 수치를 그렇게하는 것이 불법이라면 인스 털 스 멤버처럼 호출해야하는 이유는 없습니다. 객체를 생성 할 때 알고있는 것처럼 생성자를 호출 할 수 있습니다. –

+0

"'table.table (1,123)'이 클래스 범위 밖에서 유효하지 않습니다. 거기에'.' 대신'::'을 사용하여 생성자를 호출 할 수 있지만 클래스 템플릿에 대한 템플릿 매개 변수를 제공해야합니다." 편집 : 실제로 그것은 그것을 허용하는 Visual C++뿐입니다. 죄송합니다. 그러나 어쨌든 표준에서 생성자 호출에 대한 금지는 없습니다. 올바른 방법은'table (1, 123)'이다. 생성자가 int 형의 두 개의 인수를 취한다고 가정한다. –

0

: 여기 또한

template<class T> 
class table 
{ 
private: 
    T* vec_data;// initialize T 

    struct tblarray 
    { 
     std::vector<T> vecTbl[]; 
     bool operator[](unsigned int i) { return vecTbl[i]; } //redefine operator[] to accept unsigned int 
     static void operator new(double n) //redefine new operator 
     { 
      void *d; 
      if(n < 0) throw std::exception("Invalid Allocation to Negative number!"); 
      if(assert((d=malloc(n)) != 0) = 0) throw std::bad_alloc; 
      return d; 
     } 
     void operator delete(void *d) //redefine delete operator 
     { 
      if(assert((free(p))) = 0) throw std::exception("Invalid Free of specified data!"); 
     } 
     tblarray(const T&, unsigned int size) : T //one constructor 
     { 
      vecTbl = this.new std::vector<T>[reinterpret_cast<double>(size)]; 
     } 
     ~tblarray() //one destructor 
     { 
      this.delete(vecTbl); 
     } 
    }; 
public: 
    table(const T&, unsigned int size) : T 
    { 
     this.tblarray.tblarray(T, size); 
    } 
    ~table() 
    { 
     this.tblarray.~tblarray(); 
    } 
} 

는 실체화 :

int j = 1; 
table<int> t(j, 2); 

의 ctor에서 const를 T &가 중복 BTW, 유형은 이미 템플릿 매개 변수에 대입

+0

'this.delete (vecTbl);'는 컴파일되지 않습니다. 또한 다른 것들은 컴파일에 실패합니다. 그것을 게시하기 전에 * 테스트 * 코드를하시기 바랍니다. SO 대표를 쫓지 마십시오. 시간을내어. :-) –

+0

흠, 다른 많은 컴파일 오류가 있습니다. OP가 요구 한 부분을 수정했습니다. – zabulus

3

이것이 실제 코드라면 약간의 실수가 있습니다.

;를 사용하여 종료해야하지만, 난 당신의 템플릿 인스턴스화에서 보는 바와 같이 당신이 지금까지 ;

  • tblarray을 종료하지 않습니다 (구조체와 노조 포함)

      C에서
    • ++ 각 클래스, Tint이지만, tblarray T에서 파생 된 당신은 struct test : int을 가질 수 있다고 생각하십니까?

    • 모든 속성 (변수가 없습니다 : 변수 : vec_data 및 유형 : tblarray)은 비공개로 액세스 할 수 있습니까?

      C에서
    • 는 ++ this는 포인터가 그렇게이 좋은 방법이 아닙니다 (this.delete(vecTbl)operator delete(vecTbl)로 변환하여 전문 오퍼레이터를 사용 예약어 operator에 액세스하려면 this->

    • this.을 참조를 교체해야되지 않도록이다 , operator delete이 (가) 클래스의 인스턴스가 아닌 클래스 인스턴스를 삭제하도록 선언되었습니다.)

    • table은 클래스의 생성자이므로 varia를 인스턴스화 할 때이 클래스를 사용해야합니다. ble : table<...> t(1, 100) 그리고 기본이 아닌 생성자를 선언하고 기본 생성자가 없으므로 기본 생성자가 필요하므로 table<...> t을 가질 수 없습니다.

  • 관련 문제