저는 일반적으로 상호 참조를 처리하는 방법을 알고 있지만 여기서 저는 붙어 있습니다.템플릿 매개 변수를 사용할 때 교차 참조
합시다 스마트 포인터 클래스 :
template< typename T >
SharedPointer
{
T * _ptr;
};
그리고 :
class Array;
class Value
{
SharedPointer<Array> _pa;
};
그리고 : 내가 해요 SharedPointer 클래스 말에
다음class Array
{
Value someFunc();
}
난 데 경고 불완전한 유형의 객체에 대한 포인터를 삭제합니다. 이는 전방 선언으로 인한 것입니다.
Warning 2 warning C4150: deletion of pointer to incomplete type 'script::Array'; no destructor called c:\XXXXX\SharedPointer.h 77
이 문제를 해결하려면 어떻게해야합니까? 내가 볼 수있는 유일한 해결책은 같은 것으로 전체 SharedPointer 클래스를 재 작성한다 :
template< typename pT >
SharedPointer
{
pT _ptr;
};
그리고이 대신처럼 사용
SharedPointer< Array * >
나는 가능하면이 클래스를 재 작성하지 않도록하고 싶습니다. 다른 해결책이 있습니까? 는
편집을 :) 감사합니다
class Value
{
PUBLIC enum type_e
{
E_NULL,
E_INT,
E_FLOAT,
E_STRING,
E_ARRAY,
E_MAP,
E_FUNCTION,
E_REFERENCE // TODO For functions like fn(scalar & val)
};
PRIVATE union
{
int _i;
float _f;
std::string * _ps;
IFunction * _pf;
};
PRIVATE SharedPointer<Array> _pa;
PRIVATE SharedPointer<Map> _pm;
PRIVATE SharedPointer<Value> _ref;
PRIVATE type_e _type;
PUBLIC Value();
PUBLIC Value(bool b);
PUBLIC Value(int i);
PUBLIC Value(float f);
PUBLIC Value(const std::string & s);
PUBLIC Value(SharedPointer<Array> pa);
PUBLIC Value(SharedPointer<Map> pm);
PUBLIC Value(IFunction * pf);
PUBLIC Value(SharedPointer<Value> ref);
PUBLIC Value(const Value & v);
PUBLIC ~Value() { }
PUBLIC Value operator + (const Value & v) const;
PUBLIC Value operator - (const Value & v) const;
PUBLIC Value operator * (const Value & v) const;
PUBLIC Value operator/(const Value & v) const;
PUBLIC Value operator % (const Value & v) const;
PUBLIC Value operator^(const Value & v) const;
PUBLIC Value operator << (const Value & v) const;
PUBLIC Value operator -() const;
PUBLIC Value operator && (const Value & v) const;
PUBLIC Value operator || (const Value & v) const;
PUBLIC Value xor(const Value & v) const;
PUBLIC Value operator !() const;
PUBLIC Value & operator = (const Value & v);
PUBLIC Value operator() (Scope & scope, const std::vector<Value> & args) const;
PUBLIC Value & getRef(const Value & v) const;
PUBLIC Value getCpy(const Value & v) const;
PUBLIC inline type_e getType() const throw() { return _type; }
PUBLIC inline bool isNull() const throw() { return E_NULL == _type; }
PUBLIC inline bool isInt() const throw() { return E_INT == _type; }
PUBLIC inline bool isFloat() const throw() { return E_FLOAT == _type; }
PUBLIC inline bool isString() const throw() { return E_STRING == _type; }
PUBLIC inline bool isArray() const throw() { return E_ARRAY == _type; }
PUBLIC inline bool isMap() const throw() { return E_MAP == _type; }
PUBLIC inline bool isFunction() const throw() { return E_FUNCTION == _type; }
PUBLIC inline bool isReference() const throw() { return E_REFERENCE == _type; }
PUBLIC inline bool isNumeric() const throw() { return E_INT == _type || E_FLOAT == _type; }
PUBLIC type_e toNumeric(int & asInt, float & asFloat) const;
PUBLIC std::string toString() const;
PUBLIC operator bool() const throw();
};
class Array
{
PRIVATE std::vector<Value> _items;
PUBLIC Array(const std::vector<Value> & items);
PUBLIC ~Array();
PUBLIC Value & getRef(int index);
PUBLIC Value getCpy(int index) const;
PUBLIC int getSize() const;
};
template< typename T >
class SharedPointer
{
template< typename U >
friend class SharedPointer;
PRIVATE T * p;
PRIVATE size_t * c;
PUBLIC SharedPointer()
: p()
, c() { }
PUBLIC explicit SharedPointer(T * s)
: p(s)
, c(new size_t(1)) { }
PUBLIC SharedPointer(const SharedPointer & s)
: p(s.p)
, c(s.c)
{
if(this->c)
{
++*(this->c);
}
}
PUBLIC SharedPointer & operator = (const SharedPointer & s)
{
if(this != & s)
{
this->clear();
this->p = s.p;
this->c = s.c;
if(this->c)
{
++*(this->c);
}
}
return *this;
}
PUBLIC template< typename U >
SharedPointer(const SharedPointer<U> & s)
: p(s.p)
, c(s.c)
{
if(c)
{
++*(this->c);
}
}
PUBLIC ~SharedPointer()
{
this->clear();
}
PUBLIC void clear()
{
if(this->c)
{
if(*(this->c) == 1)
{
delete this->p;
}
if(! --*(this->c))
{
delete this->c;
}
}
this->c = NULL;
this->p = NULL;
}
PUBLIC T * get() const
{
return this->c ? this->p : NULL;
}
PUBLIC T * operator ->() const
{
return this->get();
}
PUBLIC T & operator *() const
{
return *(this->get());
}
};
'값'앞에 '배열'을 정의하십시오. 앞으로'Array'를 정의하기 전에'Value'를 선언하십시오. – juanchopanza
Array에 값을 정의해야하므로 할 수 없습니다. – Virus721
'Array'는'Value'의 fwd 선언만을 필요로합니다. – juanchopanza